source: branches/2.19_branch/Components/BitmapUtility.pas@ 324

Last change on this file since 324 was 15, checked in by RBRi, 19 years ago

+ components stuff

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1Unit BitmapUtility;
2
3Interface
4
5uses
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.
11Procedure CreateAutoMaskedBitmap( Var Source: TBitmap;
12 Var Mask: TBitmap );
13
14Procedure CreateMaskedBitmap( Var Source: TBitmap;
15 Var Mask: TBitmap;
16 TransparentColor: TColor );
17
18Procedure StoreBitmap( Var CurrentBitmap: TBitmap;
19 Var MaskBitmap: TBitmap;
20 const NewBitmap: TBitmap );
21
22Procedure DrawMaskedBitmap( Bitmap: TBitmap;
23 Mask: TBitmap;
24 Canvas: TCanvas;
25 X,Y: LongInt );
26
27Implementation
28
29uses
30 SysUtils;
31
32Procedure CreateAutoMaskedBitmap( Var Source: TBitmap;
33 Var Mask: TBitmap );
34var
35 TransparentColor: TColor;
36begin
37 TransparentColor:= Source.Canvas.Pixels[ 0, 0 ];
38 CreateMaskedBitmap( Source,
39 Mask,
40 TransparentColor );
41end;
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
45Procedure CreateMaskedBitmap( Var Source: TBitmap;
46 Var Mask: TBitmap;
47 TransparentColor: TColor );
48var
49 X, Y: longint;
50begin
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;
65end;
66
67Procedure StoreBitmap( Var CurrentBitmap: TBitmap;
68 Var MaskBitmap: TBitmap;
69 const NewBitmap: TBitmap );
70begin
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;
86end;
87
88Procedure DrawMaskedBitmap( Bitmap: TBitmap;
89 Mask: TBitmap;
90 Canvas: TCanvas;
91 X,Y: LongInt );
92Var
93 Source, Dest: TRect;
94Begin
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);
119End;
120
121Initialization
122End.
Note: See TracBrowser for help on using the repository browser.