Commit 6b022f9f authored by Matija Obreza's avatar Matija Obreza
Browse files

Flood fill

parent 72870a00
......@@ -747,7 +747,7 @@ export function filterOutline(image) {
dataR[P(x, y, 3)] = 255; // full alpha
// TODO fill the outline or clear the src data
shapes++;
// floodFill(dataR, w, h, x, y, color);
floodFill(dataR, w, h, x, y, color);
go = false;
break;
}
......@@ -828,41 +828,27 @@ export function floodFill(data, w, h, startX, startY, color) {
}
console.log('Fill start at', point);
stack.push(point);
// data[P(point[0], point[1], 1)] = 255; // fill start
// data[P(point[0], point[1], 3)] = 255; // fill start
const fillToEdge = (startX, startY, dir) => {
let count = 0;
for (let x = startX; x >= 0 && x < w;) {
if (data[P(x, startY, color)] === 0) {
data[P(x, startY, color)] = 255;
count++;
} else {
return count;
}
x += dir;
}
};
while ((point = stack.pop())) {
console.log('Next start at', point);
while ((point = stack.shift())) {
// console.log('Next start at', point);
const x = point[0], y = point[1];
if (data[P(x, y, color)] !== 0) {
continue;
}
const lefts = fillToEdge(x - 1, y, -1);
if (lefts) {
stack.push([x - lefts + 1, y + 1]);
} else {
stack.push([x, y + 1]);
}
const rights = fillToEdge(x + 1, y, 1);
if (rights) {
stack.push([x + rights - 1, y + 1]);
} else {
stack.push([x, y + 1]);
}
data[P(x, y, color)] = 255;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === j || i === -j) {
continue;
}
const p = P(x - j, y - i, color);
if (p >= 0) {
if (data[p] === 0) {
stack.push([ x - j, y - i]);
}
}
}
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment