-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathbricks.ts
More file actions
41 lines (40 loc) · 1.43 KB
/
bricks.ts
File metadata and controls
41 lines (40 loc) · 1.43 KB
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 { Resource } from '../load';
import { bits } from '../../utils';
export const parseBrick = (resource: Resource, index: number) => {
const dataView = new DataView(resource.getEntry(index));
const height = dataView.getUint8(1);
const offsetX = dataView.getUint8(2);
const offsetY = dataView.getUint8(3);
const buffer = new ArrayBuffer(48 * 38);
const pixels = new Uint8Array(buffer);
let ptr = 4;
for (let y = 0; y < height; y += 1) {
const numRuns = dataView.getUint8(ptr);
ptr += 1;
let x = 0;
const offset = () => ((y + offsetY) * 48) + x + offsetX;
for (let run = 0; run < numRuns; run += 1) {
const runSpec = dataView.getUint8(ptr);
ptr += 1;
const runLength = bits(runSpec, 0, 6) + 1;
const type = bits(runSpec, 6, 2);
if (type === 2) {
const color = dataView.getUint8(ptr);
ptr += 1;
for (let i = 0; i < runLength; i += 1) {
pixels[offset()] = color;
x += 1;
}
} else if (type === 1 || type === 3) {
for (let i = 0; i < runLength; i += 1) {
pixels[offset()] = dataView.getUint8(ptr);
ptr += 1;
x += 1;
}
} else {
x += runLength;
}
}
}
return pixels;
};