-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pixel): add flood fill functions
- Loading branch information
1 parent
160d47a
commit 65796b9
Showing
5 changed files
with
69 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { assert } from "@thi.ng/errors/assert"; | ||
import { floodFill } from "@thi.ng/grid-iterators/flood-fill"; | ||
import type { PackedBuffer } from "./packed.js"; | ||
|
||
/** | ||
* Fills pixel in the connected region around `x,y` with given color. Returns | ||
* pixel buffer. | ||
* | ||
* @param img | ||
* @param x | ||
* @param y | ||
* @param col | ||
*/ | ||
export const floodFillSolid = ( | ||
img: PackedBuffer, | ||
x: number, | ||
y: number, | ||
col: number | ||
) => { | ||
const { pixels, width, height } = img; | ||
const src = img.getAt(x, y); | ||
for (let [xx, yy] of floodFill( | ||
(i) => pixels[i] === src, | ||
x, | ||
y, | ||
width, | ||
height | ||
)) { | ||
pixels[yy * width + xx] = col; | ||
} | ||
return img; | ||
}; | ||
|
||
/** | ||
* Fills pixel in the connected region around `x,y` with the pattern defined by | ||
* given `pattern` image (must be in same format as `img`). Returns updated | ||
* pixel buffer. | ||
* | ||
* @param img | ||
* @param x | ||
* @param y | ||
* @param pattern | ||
*/ | ||
export const floodFillPattern = ( | ||
img: PackedBuffer, | ||
x: number, | ||
y: number, | ||
pattern: PackedBuffer | ||
) => { | ||
assert(img.format === pattern.format, "pattern must have same format"); | ||
const { pixels: dest, width, height } = img; | ||
const { pixels: src, width: sw, height: sh } = pattern; | ||
const srcCol = img.getAt(x, y); | ||
for (let [xx, yy] of floodFill( | ||
(i) => dest[i] === srcCol, | ||
x, | ||
y, | ||
width, | ||
height | ||
)) { | ||
dest[yy * width + xx] = src[(yy % sh) * sw + (xx % sw)]; | ||
} | ||
return img; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters