1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
{--- Fill ---}
procedure Fill(Img: PImage; X, Y: SmallInt; Color: TColor);
type
TSegment = record
X1, X2, Y : SmallInt;
end;
var
Stack : array of TSegment;
SP : Integer;
P : TSegment;
Background : TColor;
Left, Top, Right, Bottom : SmallInt;
{-- PushSegment --}
procedure PushSegment(const S : TSegment);
begin
if SP + 1 = Length(Stack) then
SetLength(Stack, Length(Stack) + 64);
Inc(SP);
Stack[SP] := S;
end;
{-- PopSegment --}
procedure PopSegment(out S : TSegment);
begin
S := Stack[SP];
Dec(SP);
end;
{-- ExpandLeft --}
procedure ExpandLeft(var S: TSegment);
begin
while S.X1 > Img^.fClipLeft do
if Point(Img, S.X1 - 1, S.Y) = Background then
Dec(S.X1)
else
Break;
end;
{-- ExpandRight --}
procedure ExpandRight(var S: TSegment);
begin
while S.X2 < Img^.fClipRight do
if Point(Img, S.X2 + 1, S.Y) = Background then
Inc(S.X2)
else
Break;
end;
{-- AddBgColorSegments --}
procedure AddBgColorSegments(X1, Y, X2: SmallInt);
var
I : SmallInt;
Seg : TSegment;
begin
I := X1;
while I <= X2 do
begin
if _Point(Img, I, Y) = Background then
begin
Seg.X1 := I;
Seg.X2 := I;
Seg.Y := Y;
ExpandRight(Seg);
PushSegment(Seg);
I := Seg.X2 + 1;
end
else
Inc(I);
end;
end;
begin
if not Img^.fDraw then
Exit;
if (X < Img^.fClipLeft) or (Y > Img^.fClipRight) then
Exit;
if (Y < Img^.fClipTop) or (Y > Img^.fClipBottom) then
Exit;
Background := _Point(Img, X, Y);
if Background = Color then
Exit;
if Img = _Screen then
LockScreen;
SP := -1;
Left := Img^.fWidth;
Top := Img^.fHeight;
Right := -1;
Bottom := -1;
P.X1 := X;
P.X2 := X;
P.Y := Y;
PushSegment(P);
while SP > -1 do
begin
PopSegment(P);
if (P.Y >= Img^.fClipTop) and (P.Y <= Img^.fClipBottom) then
begin
ExpandLeft(P);
ExpandRight(P);
_HLine(Img, P.X1, P.Y, P.X2, Color);
if P.X1 < Left then
Left := P.X1;
if P.X2 > Right then
Right := P.X2;
if P.Y < Top then
Top := P.Y;
if P.Y > Bottom then
Bottom := P.Y;
if P.Y - 1 > Img^.fClipTop then
AddBgColorSegments(P.X1, P.Y - 1, P.X2);
if P.Y + 1 < Img^.fClipBottom then
AddBgColorSegments(P.X1, P.Y + 1, P.X2);
end;
end;
if Img = _Screen then
begin
InvalidateRect(Left, Top, Right, Bottom);
UnlockScreen;
end;
end; |
Partager