IntroductionThe Xbitmap class adds following drawing options to the Bitmap class:
2. lines with arrows at begin,end or both (16 arrow types selectable)
3. 4 levels (0..3) of drawing
4. xdot[x,y] method for fast drawing of dots in selected penwidth
5. improved stretchdraw method
6. clipping rectangle
7. fast direct access to individual pixels with the xpixel[ ] method
8. floodfill styles of 8 x 8 or 24 x 24 pixels, 16 styles selectable
9. modification rectangle : the area that changed during the last operation
Creation and pixelformat.To create a xbitmap:
var myxbitmap : TXbitmap; ........ begin myxbitmap := TXbitmap.create; with myxbitmap do begin width := 600; height := 400; end; .....Do not use the pixelformat property.
Xbitmap only operates in the pf32bit mode and this is included in the setting of width and height.
The extra properties and methods are added to the Bitmap class, not to the canvas.
In the pf32bit pixelmode, the position of the red and blue fields in a 32 bit word are swapped compared to
the Windows color format.
function swapcolor(c : DWORD) : DWORD;where c is the color.
For the xpencolor and xbrushcolor properties, the Windows format is used and the RB colorfields are
For the xpixel[ , ] method, the color is in pf32bit format for speed purposes.
Internally, Xbitmap modifies pixels using pointers to the pixel location in memory.
Therefore it needs the pointer to row 0, column 0 and the increment value of a pointer to the next row.
These values are obtained by method xadjust when setting width or height, using the scanline[ ] property twice.
Loading the Xbitmap from memory:
with myxbitmap do begin loadfromfile(filename); xadjust; //restores the pf32bit property ... end;After a loadfromfile, xadjust must be called to insure that the pixelformat is pf32bit and to recalculate
the pointer to (0,0) and the row increment value.
Note: Pixels in a (x)bitmap are located in the computer memory and are therefor not visible.
To make a (x)bitmap visible it has to be copied to a screen canvas (such as a paintbox).
Use the canvas.draw(x,y,sbm) to copy an entire bitmap or the canvas.copyrect(dr,scv,sr) method
for a partial copy where:
sbm : the source bitmap
dr : destination rectangle
scv : the source canvas
sr : the source rectangle
1. dash-dot linesLines, ellipses and arcs may be drawn solid or dash-dotted.
The penwidth is selectable from 1 to 32 pixels.
The fatter lines are mainly needed when drawing on a bitmap that is later copied to a printer canvas.
For printers with a horizontal resolution of 2400 pixels, dimensions will be multiplied by 3 in general,
to obtain the same drawing size as on the screen.
The property xpenstyle selects the dash-dot pattern
1..19 dash-dot styles
with myxbitmap do begin xpenwidth := 3; xpenstyle:= 10; xline(20,40,120,40); ......
2. arrowsLines and arcs may have an arrow at begin or end.
There are 16 selectable arrow shapes : 4 widths and 4 lengths combined.
Fig2. below shows all arrow styles for a penwidth of 2
Bits 2,3 together with the xpenwidth make the length of the arrow.
see example below
3. drawing levels (0..3)Each pixel has a level. Level 0 is the highest- , level 3 is the lowest level.
The properties xpenlevel and xbrushlevel set the level of the pen or the brush.
A lower level pen or brush cannot alter a higher level pixel.
The level is coded in bits 0,1 of the blue color field so, bits 0 an1 of the pf32bit pixel represent the level.
Levels could be used in a drawing as
- 2: for user supplied backgrounds
- 1: grids
- 0: user supplied lines, ellipses etc.
The other colors have a lower level and never overwrite the black line.
4. xdot[x,y] method for fast drawing of dots in selected penwidthxdot(x,y) draws a dot at position (x,y) using the selected pencolor and penwidth.
This method is much faster than if using an ellipse. figure below shows some dots:
5. improved stretchdraw methodThe xstretchdraw method copies a source bitmap while adjusting it's width and/or height.
xstretchdraw may operate in two modes:
full destination (xstretchmode = smFullDest)
No part is lost. Part of the destination rectangle may not be covered.
In full destination mode, the destination rectangle is fully covered but a part of the
source rectangle may be lost.
6. clipping rectangleSometimes lines, circles or arcs must be limited to a predefined rectangle.
In the xbitmap class, this rectangle is the xcliprect : Trect property.
Default, xcliprect is the full width and height of the bitmap.
Drawing outside the xcliprect is not possible (for xline, xellipse, xarc....methods).
Picture below shows some effects:
7. fast direct access to individual pixels with the xpixel[ ] methodThe bitmap canvas has the pixel[x,y] property to read or write individual pixels.
This method is very slow because windows is called to do the job.
The xpixel[x,y] property is at least 40 times faster. The pixel is addressed directly in memory.
pf32bit colors are used, so conversion to/from the windows format must be done by
the function swapcolor(c : DWORD) : DWORD;
8. floodfill styles of 8 x 8 or 24 x 24 pixels, 16 styles selectablexfloodfill(x,y) fills the area within a shape with a selected fill pattern.
The shape must be bound by pixels of colorlevel 0.
Below are the selectable shapes
property xfillstyle : byte;//0..15selects the pattern.
xfillmapsize : Txfillmapsize;//selects fms8 or fms24the size of the filling tiles.
Use fms8 for the screen and fms24 if the bitmap is to be copied to a printer canvas.
9. modification rectangle.When a line, ellipse, arc...is drawn a certain rectangle of the bitmap is changed.
To make this change visible it must be copied to a paintbox.
Only the modified part needs copying and this part is property xmodrect (type Trect).
Xmodrect is updated after each Xbitmap action.
Sometimes rectangles of several drawing actions have to be combined.
Use the unirect function to accoplish this.
r := unirect(r,xmodrect);//combines r and xmodrectOther functions provided that operarte on rectangles
function InterRect(var r1,r2 : Trect) : Trect;//common part of rectangles function PackRect(x1,y1,x2,y2 : integer) : Trect;//coordinates to rectangle
type definitions in Xbitmap
type PDW = ^DWORD; TXFillMapSize = (fms8,fms24); TXstretchmode = (smFullSource,smFullDest);
methods in Xbitmap
Xpixel[x:integer; y:integer]:DWORD; XPixPos[x:integer;y:integer]:DWORD; Xdot(x,y : integer); Xline(x1,y1,x2,y2 : integer); Xellipse(x1,y1,x2,y2 : integer); Xarc(x1,y1,x2,y2,x3,y3,x4,y4 : integer); Xpolygon(var pa : array of Tpoint); XfillRect(x1,y1,x2,y2 : integer; r : byte); Xfloodfill(x1,y1 : integer); XstretchRect(drt : Trect; sbm : TXBitmap;srt : Trect); XAdjust;XpixPos[x,y] reads the pointer location of the pixel.
XfillRect draws a rectangle with rounded angles (0 for sharp angles) and fills with xbrushcolor.
properties in Xbitmap
XpenWidth : byte; Xpencolor : DWORD; XLineStyle : byte; Xbrushcolor : DWORD; Xpenlevel : byte; Xbrushlevel : byte; XFillMapSize : TXFillMapsize; XfillStyle : byte; XarrowCode : byte; XClipRect : Trect; XUseBrush : boolean; Xstretchmode : TXstretchmode; XModRect : Trect;Xusebrush is true for the filling of rectangles or ellipses and is false for no filling.
The Xbitmap projectProgramming language is Delpi-3.
However, the xbitmap class fits any Delphi version.
The project has the following units:
controlunit: settings,selections, handling of events
xbitmap : the xbitmap class, type definitions and some support procedures
The form is self explanatory.
The shape of a polygon may be changed by positioning the mousepointer over an angle,
pressing the mousebutton and moving.
Hold the "shift key" for 1 pixel increments instead of 10 pixel increments.
The exerciser draws pictures in the xcliprect, so first paint the xcliprect before importing a picture.
The xcliprect may the be redimensioned by placing the mousepointer over an angle
and proceding as in 1.
Custom componentsThe exerciser uses two of my own components:
- colorpicker (a grid of colors for color selection)
DownloadsAll information mentioned here is available.
Click below to download:
- the complete Delphi-3 project
- arraybutton and colorpicker components for Delphi-3