forked from cozmo/jsQR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitMatrix.ts
34 lines (29 loc) · 918 Bytes
/
BitMatrix.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
export class BitMatrix {
public static createEmpty(width: number, height: number) {
return new BitMatrix(new Uint8ClampedArray(width * height), width);
}
public width: number;
public height: number;
private data: Uint8ClampedArray;
constructor(data: Uint8ClampedArray, width: number) {
this.width = width;
this.height = data.length / width;
this.data = data;
}
public get(x: number, y: number): boolean {
if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
return false;
}
return !!this.data[y * this.width + x];
}
public set(x: number, y: number, v: boolean) {
this.data[y * this.width + x] = v ? 1 : 0;
}
public setRegion(left: number, top: number, width: number, height: number, v: boolean) {
for (let y = top; y < top + height; y++) {
for (let x = left; x < left + width; x++) {
this.set(x, y, !!v);
}
}
}
}