agglomerative-clustering 3.0.0-beta.1 → 3.0.0-beta.2
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/README.md +93 -34
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +574 -258
- package/dist/index.d.cts +13 -13
- package/dist/index.d.ts +13 -13
- package/dist/index.mjs +561 -258
- package/dist/shared.cjs +58 -3
- package/dist/shared.mjs +58 -3
- package/dist/worker.d.ts +2 -2
- package/dist/worker.mjs +281 -136
- package/package.json +1 -1
package/dist/shared.cjs
CHANGED
|
@@ -27,7 +27,7 @@ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" ==
|
|
|
27
27
|
|
|
28
28
|
// The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
|
|
29
29
|
// is built from, src/version.hpp) in step with it. Do not edit it by hand.
|
|
30
|
-
var version = exports.version = '3.0.0-beta.
|
|
30
|
+
var version = exports.version = '3.0.0-beta.2';
|
|
31
31
|
var colorSpaces = exports.colorSpaces = {
|
|
32
32
|
'rgb': 0,
|
|
33
33
|
'oklab': 1
|
|
@@ -130,6 +130,8 @@ var markPalette = exports.markPalette = function markPalette(bytes, colorSpace)
|
|
|
130
130
|
return bytes;
|
|
131
131
|
};
|
|
132
132
|
var markClustering = exports.markClustering = function markClustering(bytes) {
|
|
133
|
+
// Made by this library (or checked by `Clustering`), so its checksum needn't be checked again (see readHeader).
|
|
134
|
+
checked.add(bytes);
|
|
133
135
|
Object.defineProperty(bytes, clusteringBrand, {
|
|
134
136
|
value: true
|
|
135
137
|
});
|
|
@@ -204,12 +206,56 @@ Object.defineProperty(Clustering, Symbol.hasInstance, {
|
|
|
204
206
|
// version 2: [0xAC, 2, colour space, precision], then 9-byte records of colours
|
|
205
207
|
// version 3: [0xAC, 3, header length, record length, colour space, precision, weight unit, weight shift,
|
|
206
208
|
// total weight (u32 LE), 0, grouped error (f32 LE, from header length 17), ...], then records of
|
|
207
|
-
// colours, parent index, lighter weight and cost
|
|
209
|
+
// colours, parent index, lighter weight and cost; from 3.0.0-beta.2 the header holds a checksum (bytes 17-20)
|
|
208
210
|
var latestVersion = exports.latestVersion = 3;
|
|
209
211
|
|
|
210
212
|
// The version of stored data (clusterings) this build writes. It reads this one and format 2 (see readHeader). Unlike
|
|
211
213
|
// `version`, it changes only when the stored format does, so a 3.x release that keeps the format keeps this number.
|
|
212
214
|
var formatVersion = exports.formatVersion = latestVersion;
|
|
215
|
+
|
|
216
|
+
// ----- Checksums -----------------------------------------------------------------------------------------------------
|
|
217
|
+
// From 3.0.0-beta.2, a version 3 header holds the CRC-32 of the whole clustering (bytes 17-20, read as zeros while it is
|
|
218
|
+
// computed; see src/clustering.cpp). 0 means none: beta.1 wrote zeros there. A clustering is checked the first time it
|
|
219
|
+
// is used, and the arrays that passed (and the ones this library made) are remembered, so cutting one clustering
|
|
220
|
+
// again and again checks it once. Changing a clustering's bytes after that is not detected here, but the records are
|
|
221
|
+
// also checked as they are replayed, so it still can't give a palette of the wrong size.
|
|
222
|
+
var checksumAt = 17;
|
|
223
|
+
var crcTable = null;
|
|
224
|
+
var crc32 = function crc32(bytes) {
|
|
225
|
+
if (!crcTable) {
|
|
226
|
+
crcTable = new Uint32Array(256);
|
|
227
|
+
for (var n = 0; n < 256; n++) {
|
|
228
|
+
var c = n;
|
|
229
|
+
for (var b = 0; b < 8; b++) {
|
|
230
|
+
c = c & 1 ? 0xEDB88320 ^ c >>> 1 : c >>> 1;
|
|
231
|
+
}
|
|
232
|
+
crcTable[n] = c >>> 0;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
var crc = 0xFFFFFFFF;
|
|
236
|
+
for (var i = 0; i < bytes.length; i++) {
|
|
237
|
+
var _byte = i >= checksumAt && i < checksumAt + 4 ? 0 : bytes[i];
|
|
238
|
+
crc = crcTable[(crc ^ _byte) & 0xFF] ^ crc >>> 8;
|
|
239
|
+
}
|
|
240
|
+
return (crc ^ 0xFFFFFFFF) >>> 0;
|
|
241
|
+
};
|
|
242
|
+
var checked = new WeakSet();
|
|
243
|
+
var damaged = function damaged() {
|
|
244
|
+
return fail('invalid-clustering', "Invalid clustering: its bytes are damaged (the checksum doesn't match): it was truncated, changed or mixed up with other data after getClustering made it. Cluster the image again.", 'clustering');
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// The message for bytes that aren't a clustering: 1.x clusterings are mentioned only for bytes that could be one (no header,
|
|
248
|
+
// and a length of 0 or 3 more than a multiple of 9).
|
|
249
|
+
var notAClustering = function notAClustering(clustering) {
|
|
250
|
+
var length = clustering.length;
|
|
251
|
+
if (clustering[0] !== 0xAC && (length % 9 === 0 || length % 9 === 3)) {
|
|
252
|
+
return fail('invalid-clustering', "Invalid clustering: not a clustering from getClustering. If it was stored by agglomerative-clustering 1.x, that format is no longer read: cluster the image again.", 'clustering');
|
|
253
|
+
}
|
|
254
|
+
if (clustering[0] === 0xAC && length >= 2 && (clustering[1] === 2 || clustering[1] === 3)) {
|
|
255
|
+
return fail('invalid-clustering', "Invalid clustering: it starts like a format ".concat(clustering[1], " clustering, but its length or header doesn't fit one (").concat(length, " bytes): it was probably truncated or damaged. Cluster the image again."), 'clustering');
|
|
256
|
+
}
|
|
257
|
+
return fail('invalid-clustering', "Invalid clustering: not a clustering from getClustering (".concat(length, " bytes that don't start with a clustering header)."), 'clustering');
|
|
258
|
+
};
|
|
213
259
|
var readHeader = exports.readHeader = function readHeader(clustering) {
|
|
214
260
|
if (isPalette(clustering)) {
|
|
215
261
|
throw fail('invalid-clustering', "Invalid clustering: this is a palette, not a clustering. Palettes come from getPalette and getPaletteFromClustering; a clustering comes from getClustering.", 'clustering');
|
|
@@ -243,6 +289,15 @@ var readHeader = exports.readHeader = function readHeader(clustering) {
|
|
|
243
289
|
var merges = length < headerLength + 3 ? 0 : Math.floor((length - headerLength - 3) / recordLength);
|
|
244
290
|
var padding = length < headerLength + 3 ? length - headerLength : length - headerLength - 3 - merges * recordLength;
|
|
245
291
|
if (padding < 9) {
|
|
292
|
+
// Headers shorter than 21 bytes (from before 3.0.0-beta.1) have no checksum field. A damaged header length that
|
|
293
|
+
// makes one look that short skips this check, but then the records don't line up, which the replay reports.
|
|
294
|
+
if (!checked.has(clustering) && headerLength >= checksumAt + 4) {
|
|
295
|
+
var stored = (clustering[checksumAt] | clustering[checksumAt + 1] << 8 | clustering[checksumAt + 2] << 16 | clustering[checksumAt + 3] << 24) >>> 0;
|
|
296
|
+
if (stored !== 0 && stored !== crc32(clustering)) {
|
|
297
|
+
throw damaged();
|
|
298
|
+
}
|
|
299
|
+
checked.add(clustering);
|
|
300
|
+
}
|
|
246
301
|
return {
|
|
247
302
|
version: _version,
|
|
248
303
|
colorSpace: clustering[4],
|
|
@@ -259,7 +314,7 @@ var readHeader = exports.readHeader = function readHeader(clustering) {
|
|
|
259
314
|
throw fail('unsupported-version', "Invalid clustering: format version ".concat(_version, " is newer than this version of agglomerative-clustering reads. Upgrade the package."), 'clustering');
|
|
260
315
|
}
|
|
261
316
|
}
|
|
262
|
-
throw
|
|
317
|
+
throw notAClustering(clustering);
|
|
263
318
|
};
|
|
264
319
|
|
|
265
320
|
// Palettes are flat RGB bytes. An empty palette is allowed: it is what getPalette gives for a fully transparent
|
package/dist/shared.mjs
CHANGED
|
@@ -18,7 +18,7 @@ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" ==
|
|
|
18
18
|
|
|
19
19
|
// The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
|
|
20
20
|
// is built from, src/version.hpp) in step with it. Do not edit it by hand.
|
|
21
|
-
export var version = '3.0.0-beta.
|
|
21
|
+
export var version = '3.0.0-beta.2';
|
|
22
22
|
export var colorSpaces = {
|
|
23
23
|
'rgb': 0,
|
|
24
24
|
'oklab': 1
|
|
@@ -121,6 +121,8 @@ export var markPalette = function markPalette(bytes, colorSpace) {
|
|
|
121
121
|
return bytes;
|
|
122
122
|
};
|
|
123
123
|
export var markClustering = function markClustering(bytes) {
|
|
124
|
+
// Made by this library (or checked by `Clustering`), so its checksum needn't be checked again (see readHeader).
|
|
125
|
+
checked.add(bytes);
|
|
124
126
|
Object.defineProperty(bytes, clusteringBrand, {
|
|
125
127
|
value: true
|
|
126
128
|
});
|
|
@@ -195,12 +197,56 @@ Object.defineProperty(Clustering, Symbol.hasInstance, {
|
|
|
195
197
|
// version 2: [0xAC, 2, colour space, precision], then 9-byte records of colours
|
|
196
198
|
// version 3: [0xAC, 3, header length, record length, colour space, precision, weight unit, weight shift,
|
|
197
199
|
// total weight (u32 LE), 0, grouped error (f32 LE, from header length 17), ...], then records of
|
|
198
|
-
// colours, parent index, lighter weight and cost
|
|
200
|
+
// colours, parent index, lighter weight and cost; from 3.0.0-beta.2 the header holds a checksum (bytes 17-20)
|
|
199
201
|
export var latestVersion = 3;
|
|
200
202
|
|
|
201
203
|
// The version of stored data (clusterings) this build writes. It reads this one and format 2 (see readHeader). Unlike
|
|
202
204
|
// `version`, it changes only when the stored format does, so a 3.x release that keeps the format keeps this number.
|
|
203
205
|
export var formatVersion = latestVersion;
|
|
206
|
+
|
|
207
|
+
// ----- Checksums -----------------------------------------------------------------------------------------------------
|
|
208
|
+
// From 3.0.0-beta.2, a version 3 header holds the CRC-32 of the whole clustering (bytes 17-20, read as zeros while it is
|
|
209
|
+
// computed; see src/clustering.cpp). 0 means none: beta.1 wrote zeros there. A clustering is checked the first time it
|
|
210
|
+
// is used, and the arrays that passed (and the ones this library made) are remembered, so cutting one clustering
|
|
211
|
+
// again and again checks it once. Changing a clustering's bytes after that is not detected here, but the records are
|
|
212
|
+
// also checked as they are replayed, so it still can't give a palette of the wrong size.
|
|
213
|
+
var checksumAt = 17;
|
|
214
|
+
var crcTable = null;
|
|
215
|
+
var crc32 = function crc32(bytes) {
|
|
216
|
+
if (!crcTable) {
|
|
217
|
+
crcTable = new Uint32Array(256);
|
|
218
|
+
for (var n = 0; n < 256; n++) {
|
|
219
|
+
var c = n;
|
|
220
|
+
for (var b = 0; b < 8; b++) {
|
|
221
|
+
c = c & 1 ? 0xEDB88320 ^ c >>> 1 : c >>> 1;
|
|
222
|
+
}
|
|
223
|
+
crcTable[n] = c >>> 0;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
var crc = 0xFFFFFFFF;
|
|
227
|
+
for (var i = 0; i < bytes.length; i++) {
|
|
228
|
+
var _byte = i >= checksumAt && i < checksumAt + 4 ? 0 : bytes[i];
|
|
229
|
+
crc = crcTable[(crc ^ _byte) & 0xFF] ^ crc >>> 8;
|
|
230
|
+
}
|
|
231
|
+
return (crc ^ 0xFFFFFFFF) >>> 0;
|
|
232
|
+
};
|
|
233
|
+
var checked = new WeakSet();
|
|
234
|
+
var damaged = function damaged() {
|
|
235
|
+
return fail('invalid-clustering', "Invalid clustering: its bytes are damaged (the checksum doesn't match): it was truncated, changed or mixed up with other data after getClustering made it. Cluster the image again.", 'clustering');
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
// The message for bytes that aren't a clustering: 1.x clusterings are mentioned only for bytes that could be one (no header,
|
|
239
|
+
// and a length of 0 or 3 more than a multiple of 9).
|
|
240
|
+
var notAClustering = function notAClustering(clustering) {
|
|
241
|
+
var length = clustering.length;
|
|
242
|
+
if (clustering[0] !== 0xAC && (length % 9 === 0 || length % 9 === 3)) {
|
|
243
|
+
return fail('invalid-clustering', "Invalid clustering: not a clustering from getClustering. If it was stored by agglomerative-clustering 1.x, that format is no longer read: cluster the image again.", 'clustering');
|
|
244
|
+
}
|
|
245
|
+
if (clustering[0] === 0xAC && length >= 2 && (clustering[1] === 2 || clustering[1] === 3)) {
|
|
246
|
+
return fail('invalid-clustering', "Invalid clustering: it starts like a format ".concat(clustering[1], " clustering, but its length or header doesn't fit one (").concat(length, " bytes): it was probably truncated or damaged. Cluster the image again."), 'clustering');
|
|
247
|
+
}
|
|
248
|
+
return fail('invalid-clustering', "Invalid clustering: not a clustering from getClustering (".concat(length, " bytes that don't start with a clustering header)."), 'clustering');
|
|
249
|
+
};
|
|
204
250
|
export var readHeader = function readHeader(clustering) {
|
|
205
251
|
if (isPalette(clustering)) {
|
|
206
252
|
throw fail('invalid-clustering', "Invalid clustering: this is a palette, not a clustering. Palettes come from getPalette and getPaletteFromClustering; a clustering comes from getClustering.", 'clustering');
|
|
@@ -234,6 +280,15 @@ export var readHeader = function readHeader(clustering) {
|
|
|
234
280
|
var merges = length < headerLength + 3 ? 0 : Math.floor((length - headerLength - 3) / recordLength);
|
|
235
281
|
var padding = length < headerLength + 3 ? length - headerLength : length - headerLength - 3 - merges * recordLength;
|
|
236
282
|
if (padding < 9) {
|
|
283
|
+
// Headers shorter than 21 bytes (from before 3.0.0-beta.1) have no checksum field. A damaged header length that
|
|
284
|
+
// makes one look that short skips this check, but then the records don't line up, which the replay reports.
|
|
285
|
+
if (!checked.has(clustering) && headerLength >= checksumAt + 4) {
|
|
286
|
+
var stored = (clustering[checksumAt] | clustering[checksumAt + 1] << 8 | clustering[checksumAt + 2] << 16 | clustering[checksumAt + 3] << 24) >>> 0;
|
|
287
|
+
if (stored !== 0 && stored !== crc32(clustering)) {
|
|
288
|
+
throw damaged();
|
|
289
|
+
}
|
|
290
|
+
checked.add(clustering);
|
|
291
|
+
}
|
|
237
292
|
return {
|
|
238
293
|
version: _version,
|
|
239
294
|
colorSpace: clustering[4],
|
|
@@ -250,7 +305,7 @@ export var readHeader = function readHeader(clustering) {
|
|
|
250
305
|
throw fail('unsupported-version', "Invalid clustering: format version ".concat(_version, " is newer than this version of agglomerative-clustering reads. Upgrade the package."), 'clustering');
|
|
251
306
|
}
|
|
252
307
|
}
|
|
253
|
-
throw
|
|
308
|
+
throw notAClustering(clustering);
|
|
254
309
|
};
|
|
255
310
|
|
|
256
311
|
// Palettes are flat RGB bytes. An empty palette is allowed: it is what getPalette gives for a fully transparent
|
package/dist/worker.d.ts
CHANGED
|
@@ -33,9 +33,9 @@ export interface WorkerOptions {
|
|
|
33
33
|
* are a whole buffer of their own are transferred: a view into part of a larger buffer (a subarray, a small Node
|
|
34
34
|
* Buffer) is copied, so the caller's other data in that buffer is never detached.
|
|
35
35
|
*/
|
|
36
|
-
transfer?: boolean;
|
|
36
|
+
transfer?: boolean | undefined;
|
|
37
37
|
/** Where the worker gets its WebAssembly module; see `InitOptions`. It is sent as the worker's first call. */
|
|
38
|
-
wasm?: InitOptions['wasm'];
|
|
38
|
+
wasm?: InitOptions['wasm'] | undefined;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/** An image and its clustering, kept in the worker so re-cutting and re-mapping them copies nothing across. */
|