Advertisement
Guest User

Untitled

a guest
Dec 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #ifndef inc_gfx_flood_fill
  2. #define inc_gfx_flood_fill
  3.  
  4. #include <gfx/canvas.h>
  5. #include <smart_array.h>
  6.  
  7.     namespace gfx
  8.     {
  9.         struct floodfill_stack_t
  10.         {
  11.             floodfill_stack_t()
  12.             {
  13.                 stack.allocate(2);
  14.                 stack_pointer = 0;
  15.                 max_stack = 0;
  16.             }
  17.             bool push(int x, int y);
  18.             bool pop(int &x, int &y);
  19.             void clear()
  20.             {
  21.                 stack_pointer = 0;
  22.             }
  23.             int stack_pointer;
  24.             int max_stack;
  25.             smart_array<point> stack;
  26.         };
  27.  
  28.         void floodfill(gfx::canvas &g, floodfill_stack_t &stack, const point &pt, const argb &new_color, const argb &old_color);
  29.     }
  30.  
  31. #endif
  32.  
  33. #include "flood_fill.h"
  34.  
  35. using namespace gfx;
  36.  
  37. bool floodfill_stack_t::push(int x, int y)
  38. {
  39.     if (stack_pointer == stack.get_length() - 1) {
  40.         stack.reallocate(stack.get_length() * 2);
  41.     }
  42.  
  43.     stack[stack_pointer] = point(x, y);
  44.     stack_pointer++;
  45.  
  46.     if (stack_pointer > max_stack) {
  47.         max_stack = stack_pointer;
  48.     }
  49.  
  50.     return 1;
  51. }    
  52.  
  53. bool floodfill_stack_t::pop(int &x, int &y)
  54. {
  55.     if (stack_pointer > 0) {
  56.         x = stack[stack_pointer - 1].x;
  57.         y = stack[stack_pointer - 1].y;
  58.         stack_pointer--;
  59.         return 1;
  60.     } else {
  61.         return 0;
  62.     }    
  63. }    
  64.  
  65. void gfx::floodfill(gfx::canvas &g, floodfill_stack_t &stack, const point &pt, const argb &new_color, const argb &old_color)
  66. {
  67.     if (old_color == new_color) {
  68.         return;
  69.     }
  70.  
  71.     int x = pt.x;
  72.     int y = pt.y;
  73.  
  74.     stack.clear();
  75.  
  76.     if (!stack.push(x, y)) {
  77.         return;
  78.     }
  79.        
  80.     while (stack.pop(x, y)) {    
  81.         int y1 = y;
  82.         while (y1 >= 0) {
  83.             argb c = g.read(x, y1);
  84.             if (c != old_color) {
  85.                 break;
  86.             }
  87.             y1--;
  88.         }
  89.         y1++;
  90.                
  91.         bool spanLeft = false;
  92.         bool spanRight = false;
  93.  
  94.         while (y1 < g.height()) {
  95.             argb c = g.read(x, y1);
  96.             if (c != old_color) {
  97.                 break;
  98.             }
  99.  
  100.             g.pixel(x, y1, new_color);
  101.  
  102.             if (!spanLeft && x > 0 && g.read(x - 1, y1) == old_color) {
  103.                     if (!stack.push(x - 1, y1)) {
  104.                         return;
  105.                     }
  106.                     spanLeft = true;
  107.             } else if (spanLeft && x > 0 && g.read(x - 1, y1) != old_color) {
  108.                     spanLeft = false;
  109.             }
  110.            
  111.             if (!spanRight && x < g.width() - 1 && g.read(x + 1, y1) == old_color) {
  112.                     if (!stack.push(x + 1, y1)) {
  113.                         return;
  114.                     }
  115.                     spanRight = true;
  116.                 } else if (spanRight && x < g.width() - 1 && g.read(x + 1, y1) != old_color) {
  117.                     spanRight = false;
  118.             }
  119.             y1++;
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement