Advertisement
BlakPilar

Scan-line flood-fill

Mar 2nd, 2012
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1.         private void floodFill(Bitmap buffer, int x, int y, Color start, Color end) {
  2.             // Scan-line method -- Thanks to Runer112 for providing me with the link
  3.             // to Lode's tutorial (and thanks to Lode for writing said tutorial), which
  4.             // served as the basis for this, and jacobly for helping to fix it.
  5.  
  6.             // areColorValuesEqual(Color,Color): compares ARGB values
  7.             if (areColorValuesEqual(start, end)) return;
  8.             if (areColorValuesEqual(buffer.GetPixel(x, y), end)) return;
  9.  
  10.             // Line from current position to bottom
  11.             int y1 = y;
  12.             while (y1 < buffer.Height && areColorValuesEqual(buffer.GetPixel(x, y1), start)) {
  13.                 buffer.SetPixel(x, y1, end);
  14.                 y1++;
  15.             }
  16.             int maxY = y1 - 1;                                                          //Store the maximum Y value
  17.  
  18.             // Line from current position to top
  19.             y1 = y - 1;
  20.             while (y1 >= 0 && areColorValuesEqual(buffer.GetPixel(x, y1), start)) {
  21.                 buffer.SetPixel(x, y1, end);
  22.                 y1--;
  23.             }
  24.             int minY = y1 + 1;                                                          //Store the minimum Y value
  25.  
  26.             for (int i = minY; i < maxY; i++) {                                         //From the minimum to the maximum
  27.                 if (x > 0 && areColorValuesEqual(buffer.GetPixel(x - 1, i), start)) {   //If we can check left and it hasn't been filled left,
  28.                     floodFill(buffer, x - 1, i, start, end);                            //Fill to the left
  29.                 }
  30.                 if (x < buffer.Width - 1 && areColorValuesEqual(buffer.GetPixel(x + 1, i), start)) {//If we can go right and it hasn't been filled right,
  31.                     floodFill(buffer, x + 1, i, start, end);                            //Fill to the right
  32.                 }
  33.             }
  34.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement