/
clearBorder.ts
41 lines (39 loc) · 1.14 KB
/
clearBorder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Mask } from '..';
import { borderIterator } from '../utils/borderIterator';
import { multipleFloodFill } from './multipleFloodFill';
export interface ClearBorderOptions {
/**
* Consider pixels connected by corners?
* @default `false`
*/
allowCorners?: boolean;
/**
* Image to which the resulting image has to be put.
*/
out?: Mask;
/**
* Clear either white or black area touching the border.
* In practice it will invert the color.
* @default `'white'`
*/
color?: 'white' | 'black';
}
/**
* Set the pixels connected to the border of the mask to zero. You can chose to allow corner connection of not with the `allowCorners` option.
* @param mask - The mask to process.
* @param options - Clear border options.
* @returns The image with cleared borders.
*/
export function clearBorder(
mask: Mask,
options: ClearBorderOptions = {},
): Mask {
const { allowCorners = false, out, color = 'white' } = options;
return multipleFloodFill(mask, {
startPixels: borderIterator(mask),
startPixelValue: color === 'white' ? 1 : 0,
newPixelValue: color === 'white' ? 0 : 1,
allowCorners,
out,
});
}