-
Notifications
You must be signed in to change notification settings - Fork 5
/
drawMarker.ts
105 lines (102 loc) · 2.67 KB
/
drawMarker.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { Image } from '../Image';
import { Point } from '../geometry';
import checkProcessable from '../utils/validators/checkProcessable';
import { getDefaultColor } from '../utils/getDefaultColor';
import { getOutputImage } from '../utils/getOutputImage';
import { validateColor } from '../utils/validators/validators';
export interface DrawMarkerOptions {
/**
* Marker size, Odd number greater than 1.
* @default 1
*/
size?: number;
/**
* Marker shape.
* @default 'cross'
*/
shape?: 'circle' | 'triangle' | 'cross' | 'square';
/**
* Set marker as filled.
* @default false
*/
filled?: boolean;
/**
* Circle border color array of N elements (e.g. R, G, B or G, A), N being the number of channels.
* @default black
*/
color?: number[];
/**
* Image to which the resulting image has to be put.
*/
out?: Image;
}
/**
* Draw a marker on the image.
* @param image - Image to process.
* @param point - Marker center point.
* @param options - Draw marker options.
* @returns The image with the marker drawing.
*/
export function drawMarker(
image: Image,
point: Point,
options: DrawMarkerOptions,
): Image {
const newImage = getOutputImage(image, options, { clone: true });
const {
color = getDefaultColor(newImage),
filled = false,
shape = 'cross',
size = 1,
} = options;
validateColor(color, newImage);
checkProcessable(newImage, {
bitDepth: [8, 16],
});
if (shape === 'circle') {
newImage.drawCircle(point, size, {
color,
fill: filled ? color : undefined,
out: newImage,
});
}
if (shape === 'triangle') {
const points = [
{ row: point.row - size, column: point.column },
{ row: point.row, column: point.column + size },
{ row: point.row, column: point.column - size },
];
newImage.drawPolygon(points, {
strokeColor: color,
fillColor: filled ? color : undefined,
out: newImage,
});
}
if (shape === 'cross') {
newImage.drawLine(
{ row: point.row - size, column: point.column },
{ row: point.row + size, column: point.column },
{ strokeColor: color, out: newImage },
);
newImage.drawLine(
{ row: point.row, column: point.column - size },
{ row: point.row, column: point.column + size },
{ strokeColor: color, out: newImage },
);
}
if (shape === 'square') {
const origin = {
row: Math.floor(point.row - size / 2),
column: Math.floor(point.column - size / 2),
};
newImage.drawRectangle({
origin,
width: size,
height: size,
strokeColor: color,
fillColor: filled ? color : undefined,
out: newImage,
});
}
return newImage;
}