Janilabo

example

Jul 22nd, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.99 KB | None | 0 0
  1. // Original algorithms by slacky, optimized by Janilabo
  2.  
  3. const
  4.   FLOODFILL_8WAY = True; // Fills TPA bounds (polygon outlines)
  5.   FLOODFILL_COLOR = 65535; // ..with this color.
  6.  
  7. type
  8.   TFloodFill = (ff4Way, ff8Way);
  9.  
  10. procedure __TPAFloodFill4Way(var store: array of TBoolArray; skip: array of TBoolArray; X, Y, W, H: Integer);
  11. begin
  12.   if ((X > -1) and (Y > -1) and (X < W) and (Y < H)) then
  13.     if not skip[X][Y] then
  14.       if not store[X][Y] then
  15.       begin
  16.         store[X][Y] := True;
  17.         __TPAFloodFill4Way(store, skip, (X - 1), Y, W, H);
  18.         __TPAFloodFill4Way(store, skip, (X + 1), Y, W, H);
  19.         __TPAFloodFill4Way(store, skip, X, (Y - 1), W, H);
  20.         __TPAFloodFill4Way(store, skip, X, (Y + 1), W, H);
  21.       end;
  22. end;
  23.  
  24. procedure __TPAFloodFill8Way(var store: array of TBoolArray; skip: array of TBoolArray; X, Y, W, H: Integer);
  25. begin
  26.   if ((X > -1) and (Y > -1) and (X < W) and (Y < H)) then
  27.     if not skip[X][Y] then
  28.       if not store[X][Y] then
  29.       begin
  30.         store[X][Y] := True;
  31.         __TPAFloodFill8Way(store, skip, (X - 1), Y, W, H);
  32.         __TPAFloodFill8Way(store, skip, (X + 1), Y, W, H);
  33.         __TPAFloodFill8Way(store, skip, X, (Y - 1), W, H);
  34.         __TPAFloodFill8Way(store, skip, X, (Y + 1), W, H);
  35.         __TPAFloodFill8Way(store, skip, (X - 1), (Y - 1), W, H);
  36.         __TPAFloodFill8Way(store, skip, (X + 1), (Y + 1), W, H);
  37.         __TPAFloodFill8Way(store, skip, (X + 1), (Y - 1), W, H);
  38.         __TPAFloodFill8Way(store, skip, (X - 1), (Y + 1), W, H);
  39.       end;
  40. end;
  41.  
  42. {==============================================================================]
  43.   Explanation: Returns polygon by given outlines (shape)
  44.   NOTE: Original algorithm by slacky (warpie)
  45. [==============================================================================}
  46. function TPAFloodFill(TPAOutlines: TPointArray; start: TPoint; method: TFloodFill): TPointArray;
  47. var
  48.   i, l, r, X, Y, W, H: Integer;
  49.   b: TBox;
  50.   store, skip: array of TBoolArray;
  51. begin
  52.   l := Length(TPAOutlines);
  53.   if (l > 0) then
  54.   begin
  55.     b := IntToBox(TPAOutlines[0].X, TPAOutlines[0].Y, TPAOutlines[0].X, TPAOutlines[0].Y);
  56.     if (l > 1) then
  57.     for i := 1 to (l - 1) do
  58.     begin
  59.       if (TPAOutlines[i].X < b.X1) then
  60.         b.X1 := TPAOutlines[i].X
  61.       else
  62.         if (TPAOutlines[i].X > b.X2) then
  63.           b.X2 := TPAOutlines[i].X;
  64.       if (TPAOutlines[i].Y < b.Y1) then
  65.         b.Y1 := TPAOutlines[i].Y
  66.       else
  67.         if (TPAOutlines[i].Y > b.Y2) then
  68.           b.Y2 := TPAOutlines[i].Y;
  69.     end;
  70.     if ((start.X >= b.X1) and (start.Y >= b.Y1) and (start.X <= b.X2) and (start.Y <= b.Y2)) then
  71.     begin
  72.       W := ((b.X2 - b.X1) + 1);
  73.       H := ((b.Y2 - b.Y1) + 1);
  74.       SetLength(store, W);
  75.       SetLength(skip, W);
  76.       for i := 0 to (W - 1) do
  77.       begin
  78.         SetLength(skip[i], H);
  79.         SetLength(store[i], H);
  80.       end;
  81.       for i := 0 to (l - 1) do
  82.         skip[(TPAOutlines[i].X - b.X1)][(TPAOutlines[i].Y - b.Y1)] := True;
  83.       case method of
  84.         ff4Way: __TPAFloodFill4Way(store, skip, (start.X - b.X1), (start.Y - b.Y1), W, H);
  85.         ff8Way: __TPAFloodFill8Way(store, skip, (start.X - b.X1), (start.Y - b.Y1), W, H);
  86.       end;
  87.       SetLength(skip, 0);
  88.       SetLength(Result, (W * H));
  89.       for Y := 0 to (H - 1) do
  90.         for X := 0 to (W - 1) do
  91.           if store[X][Y] then
  92.           begin
  93.             Result[r] := Point((X + b.X1), (Y + b.Y1));
  94.             Inc(r);
  95.           end;
  96.     end;
  97.   end;
  98.   SetLength(Result, r);
  99. end;
  100.  
  101. //******* DIRTY EXAMPLE BELLOW: *******//
  102. procedure SetPixels(bmp: Integer; TPA: TPointArray; color: Integer);
  103. var
  104.   a, z, w, h: Integer;
  105. begin
  106.   z := High(TPA);
  107.   if (z > -1) then
  108.   begin
  109.     GetBitmapSize(bmp, w, h);
  110.     for a := 0 to z do
  111.       if ((TPA[a].X >= 0) and (TPA[a].Y >= 0) and (TPA[a].X < w) and (TPA[a].Y < h)) then
  112.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y, color);
  113.   end;
  114. end;
  115.  
  116. function GetBitmapColorTPA(bmp: Integer; color: Integer): TPointArray;
  117. var
  118.   w, h, x, y, r: Integer;
  119. begin
  120.   try
  121.     GetBitmapSize(bmp, w, h);
  122.   except
  123.   end;
  124.   if ((w > 0) and (h > 0)) then
  125.   begin
  126.     SetLength(Result, (w * h));
  127.     for y := 0 to (h - 1) do
  128.       for x := 0 to (w - 1) do
  129.         if (FastGetPixel(bmp, x, y) = color) then
  130.         begin
  131.           Result[r] := Point(x, y);
  132.           Inc(r);
  133.         end;
  134.   end;
  135.   SetLength(Result, r);
  136. end;
  137.  
  138. procedure Setup;
  139. var
  140.   shape, TPA: TPointArray;
  141.   bmp, w, h, t: Integer;
  142. begin
  143.   bmp := LoadBitmap(ScriptPath + 'example.bmp');
  144.   GetBitmapSize(bmp, w, h);
  145.   TPA := GetBitmapColorTPA(bmp, 0);
  146.   t := GetSystemTime;
  147.   if FLOODFILL_8WAY then
  148.     shape := TPAFloodFill(TPA, MiddleTPA(TPA), ff8Way)
  149.   else
  150.     shape := TPAFloodFill(TPA, MiddleTPA(TPA), ff4Way);
  151.   WriteLn('TPAFloodFill() finished in ' + IntToStr(GetSystemTime - t) + ' ms.');
  152.   SetPixels(bmp, shape, FLOODFILL_COLOR);
  153.   DisplayDebugImgWindow(w, h);
  154.   DrawBitmapDebugImg(bmp);
  155.   FreeBitmap(bmp);
  156.   SetLength(TPA, 0);
  157. end;
  158.  
  159. begin
  160.   Setup;
  161. end.
Advertisement
Add Comment
Please, Sign In to add comment