[15] | 1 | Unit BitmapUtility;
|
---|
| 2 |
|
---|
| 3 | Interface
|
---|
| 4 |
|
---|
| 5 | uses
|
---|
| 6 | Classes, Forms, Graphics;
|
---|
| 7 |
|
---|
| 8 | // Given the source bitmap, creates a mask bitmap for it
|
---|
| 9 | // AND masks off areas in the source so that it will draw correctly
|
---|
| 10 | // Uses bottom left pixel of the bitmap to choose transparent color.
|
---|
| 11 | Procedure CreateAutoMaskedBitmap( Var Source: TBitmap;
|
---|
| 12 | Var Mask: TBitmap );
|
---|
| 13 |
|
---|
| 14 | Procedure CreateMaskedBitmap( Var Source: TBitmap;
|
---|
| 15 | Var Mask: TBitmap;
|
---|
| 16 | TransparentColor: TColor );
|
---|
| 17 |
|
---|
| 18 | Procedure StoreBitmap( Var CurrentBitmap: TBitmap;
|
---|
| 19 | Var MaskBitmap: TBitmap;
|
---|
| 20 | const NewBitmap: TBitmap );
|
---|
| 21 |
|
---|
| 22 | Procedure DrawMaskedBitmap( Bitmap: TBitmap;
|
---|
| 23 | Mask: TBitmap;
|
---|
| 24 | Canvas: TCanvas;
|
---|
| 25 | X,Y: LongInt );
|
---|
| 26 |
|
---|
| 27 | Implementation
|
---|
| 28 |
|
---|
| 29 | uses
|
---|
| 30 | SysUtils;
|
---|
| 31 |
|
---|
| 32 | Procedure CreateAutoMaskedBitmap( Var Source: TBitmap;
|
---|
| 33 | Var Mask: TBitmap );
|
---|
| 34 | var
|
---|
| 35 | TransparentColor: TColor;
|
---|
| 36 | begin
|
---|
| 37 | TransparentColor:= Source.Canvas.Pixels[ 0, 0 ];
|
---|
| 38 | CreateMaskedBitmap( Source,
|
---|
| 39 | Mask,
|
---|
| 40 | TransparentColor );
|
---|
| 41 | end;
|
---|
| 42 |
|
---|
| 43 | // Given the source bitmap, creates a mask bitmap for it
|
---|
| 44 | // AND masks off areas in the source so that it will draw correctly
|
---|
| 45 | Procedure CreateMaskedBitmap( Var Source: TBitmap;
|
---|
| 46 | Var Mask: TBitmap;
|
---|
| 47 | TransparentColor: TColor );
|
---|
| 48 | var
|
---|
| 49 | X, Y: longint;
|
---|
| 50 | begin
|
---|
| 51 | Mask.Free;
|
---|
| 52 | Mask:= Source.Copy;
|
---|
| 53 |
|
---|
| 54 | for y:= 0 to Source.Height - 1 do
|
---|
| 55 | for X:= 0 to Source.Width - 1 do
|
---|
| 56 | begin
|
---|
| 57 | if Source.Canvas.Pixels[ X, Y ] = TransparentColor then
|
---|
| 58 | begin
|
---|
| 59 | Source.Canvas.Pixels[ X, Y ] := clBlack;
|
---|
| 60 | Mask.Canvas.Pixels[ X, Y ]:= clWhite;
|
---|
| 61 | end
|
---|
| 62 | else
|
---|
| 63 | Mask.Canvas.Pixels[ X, Y ] := clBlack;
|
---|
| 64 | end;
|
---|
| 65 | end;
|
---|
| 66 |
|
---|
| 67 | Procedure StoreBitmap( Var CurrentBitmap: TBitmap;
|
---|
| 68 | Var MaskBitmap: TBitmap;
|
---|
| 69 | const NewBitmap: TBitmap );
|
---|
| 70 | begin
|
---|
| 71 | if NewBitmap = nil then
|
---|
| 72 | begin
|
---|
| 73 | CurrentBitmap.Free;
|
---|
| 74 | CurrentBitmap:= nil;
|
---|
| 75 | MaskBitmap.Free;
|
---|
| 76 | MaskBitmap:= nil;
|
---|
| 77 | end
|
---|
| 78 | else
|
---|
| 79 | begin
|
---|
| 80 | if CurrentBitmap = nil then
|
---|
| 81 | CurrentBitmap:= TBitmap.Create;
|
---|
| 82 |
|
---|
| 83 | CurrentBitmap.LoadFromBitmap( NewBitmap );
|
---|
| 84 | CreateAutoMaskedBitmap( CurrentBitmap, MaskBitmap );
|
---|
| 85 | end;
|
---|
| 86 | end;
|
---|
| 87 |
|
---|
| 88 | Procedure DrawMaskedBitmap( Bitmap: TBitmap;
|
---|
| 89 | Mask: TBitmap;
|
---|
| 90 | Canvas: TCanvas;
|
---|
| 91 | X,Y: LongInt );
|
---|
| 92 | Var
|
---|
| 93 | Source, Dest: TRect;
|
---|
| 94 | Begin
|
---|
| 95 | If Bitmap = Nil Then
|
---|
| 96 | exit;
|
---|
| 97 |
|
---|
| 98 | Source.Left:= 0;
|
---|
| 99 | Source.Bottom:= 0;
|
---|
| 100 | Dest.Left:= X;
|
---|
| 101 | Dest.Bottom:= Y;
|
---|
| 102 |
|
---|
| 103 | if ( Mask.Width <> Bitmap.Width )
|
---|
| 104 | or ( Mask.Height <> Bitmap.Height ) then
|
---|
| 105 | raise Exception.Create( 'DrawMaskedBitmap: mask dimensions do not match bitmap' );
|
---|
| 106 |
|
---|
| 107 | Source.Right:= Mask.Width;
|
---|
| 108 | Source.Top:= Mask.Height;
|
---|
| 109 | Dest.Right:= Dest.Left + Mask.Width;
|
---|
| 110 | Dest.Top:= Dest.Bottom + Mask.Height;
|
---|
| 111 |
|
---|
| 112 | If Mask <> Nil Then
|
---|
| 113 | Mask.Canvas.BitBlt( Canvas, Dest, Source, cmSrcAnd, bitfIgnore );
|
---|
| 114 |
|
---|
| 115 | If Mask<>Nil Then
|
---|
| 116 | Bitmap.Canvas.BitBlt(Canvas,Dest,Source,cmSrcPaint,bitfIgnore)
|
---|
| 117 | Else
|
---|
| 118 | Bitmap.Canvas.BitBlt(Canvas,Dest,Source,cmSrcCopy,bitfIgnore);
|
---|
| 119 | End;
|
---|
| 120 |
|
---|
| 121 | Initialization
|
---|
| 122 | End.
|
---|