I'm working with Java.
I'm developing a paint program, the "Paint Can" tool is using a Flood Fill algorithm, but it is too expensive.
Here is the code:
private int[] dx = { -1, 0, 1, 0 };
private int[] dy = { 0, 1, 0, -1 };
public void floodFill(int x, int y, Color target_color, Color replacement_color) {
Stack<Integer[]> stack = new Stack<Integer[]>();
if (imageBuffer.getRGB(x, y) == replacement_color.getRGB())
return;
stack.push(new Integer[] { x, y });
while (!stack.isEmpty()) {
Integer[] aux = stack.peek();
imageBuffer.setRGB(aux[0], aux[1], replacement_color.getRGB());
stack.pop();
for (int i = 0; i < 4; i++) {
if (imageBuffer.getRGB(aux[0] + dx[i], aux[1] + dy[i]) == target_color.getRGB())
stack.push(new Integer[] { aux[0] + dx[i], aux[1] + dy[i] });
}
}
}
Can someone help me make this more efficient?
It takes (for 1020x700 pixel image) about 1200ms to execute.