Janilabo

floodfillex

Sep 18th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.94 KB | None | 0 0
  1. procedure TMufasaBitmap.FloodFillEx(const StartPT: TPoint; const SearchCol, ReplaceCol: TColor; 8Way: Boolean);
  2. var
  3.   Stack : TPointArray;
  4.   SIndex : Integer;
  5.   CurrX,CurrY : integer;
  6.   Search,Replace : LongWord;
  7.   procedure AddToStack(x,y : integer);inline;
  8.   begin
  9.     if LongWord(FData[y * w + x]) = Search then
  10.     begin
  11.       LongWord(FData[y * w + x]) := Replace;
  12.       Stack[SIndex].x := x;
  13.       Stack[SIndex].y := y;
  14.       inc(SIndex);
  15.     end;
  16.   end;
  17. begin
  18.   ValidatePoint(StartPT.x,StartPT.y);
  19.   Search := LongWord(RGBToBGR(SearchCol));
  20.   Replace := LongWord(RGBToBGR(ReplaceCol));
  21.   SetAlphaValue(0);
  22.   if LongWord(FData[StartPT.y * w + StartPT.x]) <> Search then //Only add items to the stack that are the searchcol.
  23.     Exit;
  24.   SetLength(Stack,w * h);
  25.   SIndex := 0;
  26.   AddToStack(StartPT.x,StartPT.y);
  27.   SIndex := 0;
  28.   case 8Way of
  29.     True:
  30.     while (SIndex >= 0) do
  31.     begin;
  32.       CurrX := Stack[SIndex].x;
  33.       Curry := Stack[SIndex].y;
  34.       if (CurrX > 0) and (CurrY > 0) then AddToStack(CurrX - 1, CurrY - 1);
  35.       if (CurrX > 0) then AddToStack(CurrX - 1, CurrY);
  36.       if (CurrX > 0) and (CurrY + 1 < h) then AddToStack(CurrX - 1, CurrY + 1);
  37.       if (CurrY + 1 < h) then AddToStack(CurrX , CurrY + 1);
  38.       if (CurrX + 1 < w) and (CurrY + 1 < h) then AddToStack(CurrX + 1, CurrY + 1);
  39.       if (CurrX + 1 < w) then AddToStack(CurrX + 1, CurrY );
  40.       if (CurrX + 1 < w) and (CurrY > 0) then AddToStack(CurrX + 1, CurrY - 1);
  41.       if (CurrY > 0) then AddToStack(CurrX , CurrY - 1);
  42.       Dec(SIndex);
  43.     end;
  44.     False:
  45.     while (SIndex >= 0) do
  46.     begin;
  47.       CurrX := Stack[SIndex].x;
  48.       Curry := Stack[SIndex].y;
  49.       if (CurrX > 0) then AddToStack(CurrX - 1, CurrY);
  50.       if (CurrY + 1 < h) then AddToStack(CurrX , CurrY + 1);
  51.       if (CurrX + 1 < w) then AddToStack(CurrX + 1, CurrY );
  52.       if (CurrY > 0) then AddToStack(CurrX , CurrY - 1);
  53.       Dec(SIndex);
  54.     end;
  55.   end;
  56. end;
Advertisement
Add Comment
Please, Sign In to add comment