@thi.ng/rle-pack 3.1.43 → 3.1.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/index.js +86 -99
- package/package.json +9 -6
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -1,111 +1,98 @@
|
|
|
1
1
|
import { BitInputStream } from "@thi.ng/bitstream/input";
|
|
2
2
|
import { BitOutputStream } from "@thi.ng/bitstream/output";
|
|
3
3
|
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
|
|
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
|
-
out.write(t, 2);
|
|
43
|
-
out.write(m, rleSizes[t]);
|
|
44
|
-
out.writeWords(chunk, wordSize);
|
|
45
|
-
chunk.length = 0;
|
|
46
|
-
};
|
|
47
|
-
for (let x of src) {
|
|
48
|
-
if (val === undefined) {
|
|
49
|
-
val = x;
|
|
50
|
-
}
|
|
51
|
-
else if (x !== val) {
|
|
52
|
-
if (n > 0) {
|
|
53
|
-
writeRLE();
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
chunk.push(val);
|
|
57
|
-
if (chunk.length === rle3) {
|
|
58
|
-
writeChunk();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
val = x;
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
if (chunk.length) {
|
|
65
|
-
writeChunk();
|
|
66
|
-
}
|
|
67
|
-
if (++n === rle3) {
|
|
68
|
-
n--;
|
|
69
|
-
writeRLE();
|
|
70
|
-
tail = i < n1;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
if (i === n1) {
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
i++;
|
|
77
|
-
}
|
|
78
|
-
if (chunk.length) {
|
|
4
|
+
const encode = (src, num, wordSize = 8, rleSizes = [3, 4, 8, 16]) => {
|
|
5
|
+
(wordSize < 1 || wordSize > 32) && illegalArgs("word size (1-32 bits only)");
|
|
6
|
+
const out = new BitOutputStream(Math.ceil(num * wordSize / 8) + 4 + 2 + 1).write(num, 32).write(wordSize - 1, 5);
|
|
7
|
+
rleSizes.forEach((x) => {
|
|
8
|
+
(x < 1 || x > 16) && illegalArgs("RLE repeat size (1-16 bits only)");
|
|
9
|
+
out.write(x - 1, 4);
|
|
10
|
+
});
|
|
11
|
+
const [rle0, rle1, rle2, rle3] = rleSizes.map((x) => 1 << x);
|
|
12
|
+
const chunk = [];
|
|
13
|
+
const n1 = num - 1;
|
|
14
|
+
let val;
|
|
15
|
+
let tail = true;
|
|
16
|
+
let n = 0;
|
|
17
|
+
let i = 0;
|
|
18
|
+
const writeRLE = () => {
|
|
19
|
+
const t = n < rle0 ? 0 : n < rle1 ? 1 : n < rle2 ? 2 : 3;
|
|
20
|
+
out.writeBit(1);
|
|
21
|
+
out.write(t, 2);
|
|
22
|
+
out.write(n, rleSizes[t]);
|
|
23
|
+
out.write(val, wordSize);
|
|
24
|
+
n = 0;
|
|
25
|
+
};
|
|
26
|
+
const writeChunk = () => {
|
|
27
|
+
const m = chunk.length - 1;
|
|
28
|
+
const t = m < rle0 ? 0 : m < rle1 ? 1 : m < rle2 ? 2 : 3;
|
|
29
|
+
out.writeBit(0);
|
|
30
|
+
out.write(t, 2);
|
|
31
|
+
out.write(m, rleSizes[t]);
|
|
32
|
+
out.writeWords(chunk, wordSize);
|
|
33
|
+
chunk.length = 0;
|
|
34
|
+
};
|
|
35
|
+
for (let x of src) {
|
|
36
|
+
if (val === void 0) {
|
|
37
|
+
val = x;
|
|
38
|
+
} else if (x !== val) {
|
|
39
|
+
if (n > 0) {
|
|
40
|
+
writeRLE();
|
|
41
|
+
} else {
|
|
79
42
|
chunk.push(val);
|
|
43
|
+
if (chunk.length === rle3) {
|
|
44
|
+
writeChunk();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
val = x;
|
|
48
|
+
} else {
|
|
49
|
+
if (chunk.length) {
|
|
80
50
|
writeChunk();
|
|
81
|
-
|
|
82
|
-
|
|
51
|
+
}
|
|
52
|
+
if (++n === rle3) {
|
|
53
|
+
n--;
|
|
83
54
|
writeRLE();
|
|
55
|
+
tail = i < n1;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (i === n1) {
|
|
59
|
+
break;
|
|
84
60
|
}
|
|
85
|
-
|
|
61
|
+
i++;
|
|
62
|
+
}
|
|
63
|
+
if (chunk.length) {
|
|
64
|
+
chunk.push(val);
|
|
65
|
+
writeChunk();
|
|
66
|
+
} else if (tail) {
|
|
67
|
+
writeRLE();
|
|
68
|
+
}
|
|
69
|
+
return out.bytes();
|
|
86
70
|
};
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
}
|
|
71
|
+
const decode = (src) => {
|
|
72
|
+
const input = new BitInputStream(src);
|
|
73
|
+
const num = input.read(32);
|
|
74
|
+
const wordSize = input.read(5) + 1;
|
|
75
|
+
const rleSizes = [0, 0, 0, 0].map(() => input.read(4) + 1);
|
|
76
|
+
const out = arrayForWordSize(wordSize, num);
|
|
77
|
+
let x, j;
|
|
78
|
+
for (let i = 0; i < num; ) {
|
|
79
|
+
x = input.readBit();
|
|
80
|
+
j = i + 1 + input.read(rleSizes[input.read(2)]);
|
|
81
|
+
if (x) {
|
|
82
|
+
out.fill(input.read(wordSize), i, j);
|
|
83
|
+
i = j;
|
|
84
|
+
} else {
|
|
85
|
+
for (; i < j; i++) {
|
|
86
|
+
out[i] = input.read(wordSize);
|
|
87
|
+
}
|
|
106
88
|
}
|
|
107
|
-
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
108
91
|
};
|
|
109
92
|
const arrayForWordSize = (ws, n) => {
|
|
110
|
-
|
|
93
|
+
return new (ws < 9 ? Uint8Array : ws < 17 ? Uint16Array : Uint32Array)(n);
|
|
94
|
+
};
|
|
95
|
+
export {
|
|
96
|
+
decode,
|
|
97
|
+
encode
|
|
111
98
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/rle-pack",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.45",
|
|
4
4
|
"description": "Binary run-length encoding packer w/ flexible repeat bit widths",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -33,11 +35,12 @@
|
|
|
33
35
|
"test": "bun test"
|
|
34
36
|
},
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"@thi.ng/bitstream": "^2.2.
|
|
37
|
-
"@thi.ng/errors": "^2.4.
|
|
38
|
+
"@thi.ng/bitstream": "^2.2.37",
|
|
39
|
+
"@thi.ng/errors": "^2.4.7"
|
|
38
40
|
},
|
|
39
41
|
"devDependencies": {
|
|
40
42
|
"@microsoft/api-extractor": "^7.38.3",
|
|
43
|
+
"esbuild": "^0.19.8",
|
|
41
44
|
"rimraf": "^5.0.5",
|
|
42
45
|
"tools": "^0.0.1",
|
|
43
46
|
"typedoc": "^0.25.4",
|
|
@@ -57,7 +60,7 @@
|
|
|
57
60
|
"access": "public"
|
|
58
61
|
},
|
|
59
62
|
"engines": {
|
|
60
|
-
"node": ">=
|
|
63
|
+
"node": ">=18"
|
|
61
64
|
},
|
|
62
65
|
"files": [
|
|
63
66
|
"./*.js",
|
|
@@ -76,5 +79,5 @@
|
|
|
76
79
|
],
|
|
77
80
|
"year": 2017
|
|
78
81
|
},
|
|
79
|
-
"gitHead": "
|
|
82
|
+
"gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n"
|
|
80
83
|
}
|