2009-08-13 123 views
2

我一直在使用基於堆棧的非遞歸類型的floodfill算法,它似乎完美地工作,除了對於一個令人討厭的情況:如果使用一條線將圖像剪成一半然後填充一半,整個圖像!然而,只有當我不在圖像周圍放置「邊框」時,纔會發生這種情況。如果我繪製一個封裝圖像的矩形(即在圖像上放置邊框),那麼它可以正常工作。所以很明顯,代碼的邊界查找有問題,但我不能爲我的生活找到問題。有人能夠(希望)比我更敏銳地發現問題嗎?這讓我瘋狂! (PS語言是C)這個floodfill算法有什麼問題?

/** scanfill algorithm **/ 
/* the stack */ 
#define stackSize 16777218 
int stack[stackSize]; 
int stackPointer; 

static bool 
pop(int * x, int * y, int h) 
{ 
    if(stackPointer > 0) 
    { 
     int p = stack[stackPointer]; 
     *x = p/h; 
     *y = p % h; 
     stackPointer--; 
     return true; 
    }  
    else 
    { 
     return false; 
    }  
}  

static bool 
push(int x, int y, int h) 
{ 
    if(stackPointer < stackSize - 1) 
    { 
     stackPointer++; 
     stack[stackPointer] = h * x + y; 
     return true; 
    }  
    else 
    { 
     return false; 
    }  
}  

static void 
emptyStack() 
{ 
    int x, y; 
    while(pop(&x, &y, 0)); 
} 

void 
scan_fill_do_action(int x, int y, texture_info * tex, VALUE hash_arg, 
       sync sync_mode, bool primary, action_struct * payload) 
{ 
    action_struct cur; 
    rgba old_color; 
    int y1; 
    bool spanLeft, spanRight; 

    if(!bound_by_rect(x, y, 0, 0, tex->width - 1, tex->height - 1)) return; 

    draw_prologue(&cur, tex, 0, 0, 1024, 1024, &hash_arg, sync_mode, primary, &payload); 

    old_color = get_pixel_color(tex, x, y); 

    if(cmp_color(old_color, cur.color)) return; 

    emptyStack(); 

    if(!push(x, y, tex->width)) return; 

    while(pop(&x, &y, tex->width)) 
    {  
     y1 = y; 
     while(y1 >= 0 && cmp_color(old_color, get_pixel_color(tex, x, y1))) y1--; 
     y1++; 
     spanLeft = spanRight = false; 
     while(y1 < tex->height && cmp_color(old_color, get_pixel_color(tex, x, y1))) 
      { 
       set_pixel_color_with_style(payload, tex, x, y1); 

       if(!spanLeft && x > 0 && cmp_color(old_color, get_pixel_color(tex, x - 1, y1))) 
        { 
         if(!push(x - 1, y1, tex->width)) return; 
         spanLeft = true; 
        } 
       else if(spanLeft && x > 0 && !cmp_color(old_color, get_pixel_color(tex, x - 1, y1))) 
        { 
         spanLeft = false; 
        } 


       if(!spanRight && x < tex->width && cmp_color(old_color, 
                   get_pixel_color(tex, x + 1, y1))) 
        { 
         if(!push(x + 1, y1, tex->width)) return; 
         spanRight = true; 
        } 

       else if(spanRight && x < tex->width && !cmp_color(old_color, 
                     get_pixel_color(tex, x + 1, y1))) 
        { 
         spanRight = false; 
        } 
       y1++; 
      } 
    } 
    draw_epilogue(&cur, tex, primary); 
} 
+0

你解決了嗎?因爲這是我的問題了! – Farsheed 2014-02-28 18:30:01

回答

3

我在它只是有一個短暫的一瞥,但似乎你在

if(!spanRight && x < tex->width && ... 

`和

else if(spanRight && x < tex->width && ... 

將有一個邊界包裹行應讀取

if(!spanRight && x < tex->width-1 && ... 
    else if(spanRight && x < tex->width-1 && ... 
+0

我已經試過這個:(它並沒有解決問題:((( – horseyguy 2009-08-13 09:37:50