模式洪水填充算法

时间:2016-01-22 22:47:54

标签: c algorithm graphics

我已经实现了一个泛色填充算法,该算法可以正确地用于纯色填充。现在我正在进行模式填充,并决定添加一个标志来查看如何填充区域(使用颜色或图案)。然而,当使用带有图案填充的算法时,在绘制区域时会卡住。

这是我使用纯色的原始代码:

void floodFillStack(int x, int y, byte newColor, byte oldColor) {
    int y1;
    if (oldColor == newColor) return;
    if (get_pixel(x, y) != oldColor) return;
    //draw current scanline from start position to the top 
    y1 = y;
    while (y1 < h && get_pixel(x, y1) == oldColor) {
        plot_pixel(x, y1, newColor);
        y1++;
    }    

    //draw current scanline from start position to the bottom
    y1 = y - 1;
    while (y1 >= 0 && get_pixel(x, y1) == oldColor) {
        plot_pixel(x, y1, newColor);
        y1--;
    }

    //test for new scanlines to the left
    y1 = y;
    while (y1 < h && get_pixel(x, y1) == newColor) {
        if (x > 0 && get_pixel(x - 1, y1) == oldColor) {
            floodFillStack(x - 1, y1, newColor, oldColor);
        } 
        y1++;
    }
    y1 = y - 1;
    while (y1 >= 0 && get_pixel(x, y1) == newColor) {
        if (x > 0 && get_pixel(x - 1, y1) == oldColor) {
            floodFillStack(x - 1, y1, newColor, oldColor);
        }
        y1--;
    } 

    //test for new scanlines to the right 
    y1 = y;
    while (y1 < h && get_pixel(x, y1) == newColor) {
        if (x < w - 1 && get_pixel(x + 1, y1) == oldColor) {           
            floodFillStack(x + 1, y1, newColor, oldColor);
        } 
        y1++;
    }
    y1 = y - 1;
    while (y1 >= 0 &&get_pixel(x, y1) == newColor) {
        if (x < w - 1 && get_pixel(x + 1, y1) == oldColor) {
            floodFillStack(x + 1, y1, newColor, oldColor);
        }
        y1--;
    }
}

这是模式修改(它仍然适用于纯色)。

int pattern1[6][6] = { {0,1,0,1,0,1},
                       {1,0,1,0,1,0},
                       {0,1,0,1,0,1},
                       {1,0,1,0,1,0},
                       {0,1,0,1,0,1},
                       {1,0,1,0,1,0} };             
int pattern2[6][6] = { {0,0,1,1,0,0},
                       {0,1,0,0,1,0},
                       {1,0,0,0,0,1},
                       {1,0,0,0,0,1},
                       {0,1,0,0,1,0},
                       {0,0,1,1,0,0} };

void floodFillStack(int x, int y, byte newColor, byte oldColor, int pattern_fill) {
    int y1;
    if (oldColor == newColor) return;
    if (get_pixel(x, y) != oldColor) return;
    //draw current scanline from start position to the top 
    y1 = y;
    while (y1 < h && get_pixel(x, y1) == oldColor) {
        if (pattern_fill == 0) {
            if (fill_pattern == 1) {
                if (pattern1[x%6][y1%6] == 1) {
                    plot_pixel(x, y1, newColor);
                }
            } else {
                if (pattern2[x%6][y1%6] == 1) {
                    plot_pixel(x, y1, newColor);    
                }               
            }
        } else {                
            plot_pixel(x, y1, newColor);
        }
        y1++;
    }    
    //draw current scanline from start position to the bottom
    y1 = y - 1;
    while (y1 >= 0 && get_pixel(x, y1) == oldColor) {
        if (pattern_fill == 0) {
            if (fill_pattern == 1) {
                if (pattern1[x%6][y1%6] == 1) {
                    plot_pixel(x, y1, newColor);
                }
            } else {
                if (pattern2[x%6][y1%6] == 1) {
                    plot_pixel(x, y1, newColor);
                }               
            }
        } else {
            plot_pixel(x, y1, newColor);
        }
        y1--;
    }
    //test for new scanlines to the left
    y1 = y;
    while (y1 < h && get_pixel(x, y1) == newColor) {
        if (x > 0 && get_pixel(x - 1, y1) == oldColor) {
            floodFillStack(x - 1, y1, newColor, oldColor, pattern_fill);
        } 
        y1++;
    }
    y1 = y - 1;
    while (y1 >= 0 && get_pixel(x, y1) == newColor) {
        if (x > 0 && get_pixel(x - 1, y1) == oldColor) {
            floodFillStack(x - 1, y1, newColor, oldColor, pattern_fill);
        }
        y1--;
    } 
    //test for new scanlines to the right 
    y1 = y;
    while (y1 < h && get_pixel(x, y1) == newColor) {
        if (x < w - 1 &&get_pixel(x + 1, y1) == oldColor) {           
            floodFillStack(x + 1, y1, newColor, oldColor, pattern_fill);
        } 
        y1++;
    }
    y1 = y - 1;
    while (y1 >= 0 && get_pixel(x, y1) == newColor) {
        if (x < w - 1 && get_pixel(x + 1, y1) == oldColor) {
            floodFillStack(x + 1, y1, newColor, oldColor, pattern_fill);
        }
        y1--;
    }
}

任何人都可以帮我看看问题吗?

修改

感谢WeatherVane的建议。算法不再卡住,但它不会覆盖整个区域。这是一张图片:

enter image description here

2 个答案:

答案 0 :(得分:1)

当您填充纯色时,从oldColor开始的每个像素都会更改为newColor。这意味着稍后在该过程中对该像素的测试将不再与之匹配。

当您尝试填充图案时,其中一些像素将保留为oldColor。你陷入无限循环,一遍又一遍地重新测试那些相同的像素。

答案 1 :(得分:1)

如果您忽略模式蒙版中的0值,则您的填充可能会失败。而不是那样,总是填充两种颜色中的一种(两种颜色都与背景不同)。

您还必须更改一些条件测试。而不是

(... == newColor)

你可以使用

(... != oldColor)

(... == newColor1 || ... == newColor2)