agglomerative-clustering 4.0.0-beta.3 → 4.0.0-beta.4
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 +1 -1
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +26 -194
- package/dist/index.mjs +16 -184
- package/dist/shared.cjs +216 -8
- package/dist/shared.mjs +208 -6
- package/dist/worker-thread.mjs +1 -1
- package/dist/worker.cjs +129 -78
- package/dist/worker.mjs +130 -79
- package/package.json +1 -1
package/dist/worker.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// handle, so re-cutting and re-mapping it copies nothing across. The functions exported directly use one shared
|
|
5
5
|
// worker, started on first use.
|
|
6
6
|
|
|
7
|
-
import { version, fail, isCodedError, detachedError, isOptionsObject, isUint8Array, isUint8ClampedArray, snapshot,
|
|
7
|
+
import { version, fail, isCodedError, detachedError, isOptionsObject, isUint8Array, isUint8ClampedArray, snapshot, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge, bareImageError, readHeader, checkImageBody, imageCopy, checkK, checkPalette, checkNotEmpty, checkChoice, checkPixelArray, parseOptions, clusterOptions as clusterKeys, quantizeOptions as quantizeKeys, mapOptions as mapKeys, mapIndexOptions as mapIndexKeys } from "./shared.mjs";
|
|
8
8
|
|
|
9
9
|
// The same version, types and data-format version as the main entry (one implementation, in shared.js).
|
|
10
10
|
export { version, Palette, Clustering, errorCodes, isCodedError, ditherMethods, ditherSteps, colorSpaces, imageFormats, alphaModes } from "./shared.mjs";
|
|
@@ -138,7 +138,7 @@ const start = async (receive, chosen) => {
|
|
|
138
138
|
// A page with an opaque origin (a sandboxed iframe without allow-same-origin, as plugin hosts such as Figma use) can't
|
|
139
139
|
// start a worker from any http(s) script, nor, in Chromium, from a blob: URL; a data: URL module can, and imports it.
|
|
140
140
|
const opaque = !crossOrigin && isOpaqueOrigin() && webOrigins.includes(script.protocol);
|
|
141
|
-
const scriptHint = "Check that worker-thread.mjs is served (open its URL), with index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it, which it imports, or say where your copy of dist/ is with createWorker({ script }) or, for the shared worker, init({ script }).";
|
|
141
|
+
const scriptHint = "Check that worker-thread.mjs is served (open its URL), with index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it, which it imports, or say where your copy of dist/ is with createWorker({ script }) or, for the shared worker, init({ script }). The worker's files can also be from different versions of agglomerative-clustering: serve all of dist/ from this version.";
|
|
142
142
|
let worker,
|
|
143
143
|
source = null;
|
|
144
144
|
try {
|
|
@@ -195,11 +195,12 @@ const start = async (receive, chosen) => {
|
|
|
195
195
|
});
|
|
196
196
|
worker.on('message', receive);
|
|
197
197
|
// A worker script that can't be found, or that can't find what it imports: a copy of dist/ split up or bundled away.
|
|
198
|
+
const versions = " The worker's files can also be from different versions of agglomerative-clustering: serve all of dist/ from this version.";
|
|
198
199
|
const missing = error => error && (error.code === 'ERR_MODULE_NOT_FOUND' || /Cannot find module|import\.meta' outside a module|Qualified path resolution failed/.test(String(error.message)));
|
|
199
200
|
// Yarn Plug'n'Play keeps packages in zip archives, which Node reads ES modules from only through Yarn's loader.
|
|
200
201
|
const inZip = error => /\.zip[\\/]/.test(String(error && error.message));
|
|
201
202
|
worker.on('error', error => receive({
|
|
202
|
-
failure: missing(error) ? inZip(error) ? `${String(error.message).split('\n')[0]}. Under Yarn Plug'n'Play the package sits in a zip archive, which a worker thread can load its ES modules from only through Yarn's loader: run the app with yarn node (or node --require ./.pnp.cjs --loader ./.pnp.loader.mjs), or unplug the package (yarn unplug agglomerative-clustering). Otherwise keep dist/ together and say where it is with createWorker({ script })
|
|
203
|
+
failure: missing(error) ? inZip(error) ? `${String(error.message).split('\n')[0]}. Under Yarn Plug'n'Play the package sits in a zip archive, which a worker thread can load its ES modules from only through Yarn's loader: run the app with yarn node (or node --require ./.pnp.cjs --loader ./.pnp.loader.mjs), or unplug the package (yarn unplug agglomerative-clustering). Otherwise keep dist/ together and say where it is with createWorker({ script }).${versions}` : `${String(error.message).split('\n')[0]}. The worker script (worker-thread.mjs) needs index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it: keep dist/ together, and say where it is with createWorker({ script }) or, for the shared worker, init({ script }) (a path, or a file: URL). In a serverless deployment (AWS Lambda, Vercel or Netlify functions), the file tracer may have left them out: include node_modules/agglomerative-clustering/dist in the function's files.${versions}` : error.message
|
|
203
204
|
}));
|
|
204
205
|
worker.on('exit', code => receive({
|
|
205
206
|
failure: `The worker exited unexpectedly (code ${code}).`
|
|
@@ -258,6 +259,10 @@ const send = image => {
|
|
|
258
259
|
// An image object goes through the main entry's own checks here (its fields read once, then shape, data, detached buffer, format,
|
|
259
260
|
// sizes, unknown keys, premultiplied), so what can't be sent is reported as the main entry reports it, before anything is posted.
|
|
260
261
|
// A function, a Symbol or a thenable gets the shape error rather than the structured clone's.
|
|
262
|
+
// A bare array has to be what the main entry accepts as one (the checks it makes before its options).
|
|
263
|
+
if (ArrayBuffer.isView(image)) {
|
|
264
|
+
checkPixelArray(image);
|
|
265
|
+
}
|
|
261
266
|
if (!ArrayBuffer.isView(image) && !isImageData(image)) {
|
|
262
267
|
const copy = imageCopy(image);
|
|
263
268
|
if (copy === image) {
|
|
@@ -419,81 +424,68 @@ const initOptions = (options, allowScript) => {
|
|
|
419
424
|
return checked;
|
|
420
425
|
};
|
|
421
426
|
|
|
427
|
+
// What the options of a call are checked against: the image's data length, format and width (a bare array is RGBA).
|
|
428
|
+
const imageShape = sent => ArrayBuffer.isView(sent) ? {
|
|
429
|
+
data: sent,
|
|
430
|
+
format: 'rgba',
|
|
431
|
+
width: undefined
|
|
432
|
+
} : {
|
|
433
|
+
data: sent.data,
|
|
434
|
+
format: sent.format,
|
|
435
|
+
width: sent.width
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
// Checks a call's options exactly as the main entry does (the same parseOptions, `allowed` being the keys the function takes,
|
|
439
|
+
// `image` the shape of the image it maps, if any), so a wrong option is reported here, in the main entry's order and words,
|
|
440
|
+
// before anything is posted or a buffer is transferred. What it returns is what is sent: the options as one plain copy, and
|
|
441
|
+
// `parsed`, what parseOptions made of them.
|
|
422
442
|
// A signal and a progress callback can't cross to the worker, so they stay on this side: the options are sent without
|
|
423
443
|
// them, a cancel message is sent when the signal aborts, and the worker's progress messages call the callback.
|
|
424
|
-
const takeControls = (options,
|
|
425
|
-
|
|
426
|
-
// once first: structured cloning drops inherited properties, and the copy is what gets validated and sent.
|
|
427
|
-
if (!isOptionsObject(options)) {
|
|
444
|
+
const takeControls = (options, allowed, image) => {
|
|
445
|
+
if (options === undefined) {
|
|
428
446
|
return {
|
|
429
447
|
options,
|
|
430
448
|
signal: undefined,
|
|
431
|
-
onProgress: undefined
|
|
449
|
+
onProgress: undefined,
|
|
450
|
+
parsed: parseOptions(options, allowed, image)
|
|
432
451
|
};
|
|
433
452
|
}
|
|
453
|
+
// Anything but an ordinary object is rejected by parseOptions, as in the main entry. An ordinary one is copied once first:
|
|
454
|
+
// structured cloning drops inherited properties, and the copy is what gets validated and sent.
|
|
455
|
+
if (!isOptionsObject(options)) {
|
|
456
|
+
parseOptions(options, allowed, image);
|
|
457
|
+
}
|
|
434
458
|
options = snapshot(options);
|
|
435
459
|
// The dither setting is an object of its own, which structured cloning would copy without its inherited keys (or not
|
|
436
|
-
// at all, for a Proxy or a function): read it here the way the main entry does, and send
|
|
437
|
-
// A function that takes no dither option rejects the key whatever its value, as the main entry does: send a value that
|
|
438
|
-
// can cross, for the worker's validation to name the option.
|
|
439
|
-
if (!ditherAllowed && 'dither' in options) {
|
|
440
|
-
options.dither = null;
|
|
441
|
-
}
|
|
460
|
+
// at all, for a Proxy or a function): read it here the way the main entry does, once, and validate and send the copy.
|
|
442
461
|
if (options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
|
|
443
|
-
|
|
444
|
-
const copy = isOptionsObject(options.dither) ? Object.assign({}, snapshot(options.dither)) : options.dither;
|
|
445
|
-
// A wrong setting is the worker's to report, after the image and k, as the main entry orders its checks. Only a
|
|
446
|
-
// copy that can't be sent is reported here.
|
|
447
|
-
try {
|
|
448
|
-
parseDither(copy);
|
|
449
|
-
} catch (error) {
|
|
450
|
-
if (!sendable(copy)) {
|
|
451
|
-
throw error;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
options.dither = copy;
|
|
462
|
+
options.dither = isOptionsObject(options.dither) ? Object.assign({}, snapshot(options.dither)) : options.dither;
|
|
455
463
|
}
|
|
456
|
-
|
|
457
|
-
//
|
|
458
|
-
//
|
|
459
|
-
// those messages not echoing the value: if they ever say what they got, send a stand-in that reads the same.)
|
|
464
|
+
const parsed = parseOptions(options, allowed, image);
|
|
465
|
+
// Every value left is a primitive, a copy of the dither object or the signal and callback taken out here, so what is
|
|
466
|
+
// posted can be cloned. (The `cannot be sent` error in `unsendable` stays as a safety net for anything that still isn't.)
|
|
460
467
|
const {
|
|
461
468
|
signal,
|
|
462
469
|
onProgress
|
|
463
470
|
} = options;
|
|
464
|
-
const badSignal = signal !== undefined && (signal === null || typeof signal !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function');
|
|
465
|
-
const badProgress = onProgress !== undefined && typeof onProgress !== 'function';
|
|
466
471
|
const {
|
|
467
472
|
signal: _signal,
|
|
468
473
|
onProgress: _onProgress,
|
|
469
474
|
...rest
|
|
470
475
|
} = options;
|
|
471
|
-
if (badSignal || badProgress) {
|
|
472
|
-
return {
|
|
473
|
-
options: {
|
|
474
|
-
...rest,
|
|
475
|
-
...(badSignal ? {
|
|
476
|
-
signal: null
|
|
477
|
-
} : {}),
|
|
478
|
-
...(badProgress ? {
|
|
479
|
-
onProgress: null
|
|
480
|
-
} : {})
|
|
481
|
-
},
|
|
482
|
-
signal: undefined,
|
|
483
|
-
onProgress: undefined
|
|
484
|
-
};
|
|
485
|
-
}
|
|
486
476
|
if (signal === undefined && onProgress === undefined) {
|
|
487
477
|
return {
|
|
488
478
|
options,
|
|
489
479
|
signal,
|
|
490
|
-
onProgress
|
|
480
|
+
onProgress,
|
|
481
|
+
parsed
|
|
491
482
|
};
|
|
492
483
|
}
|
|
493
484
|
return {
|
|
494
485
|
options: rest,
|
|
495
486
|
signal,
|
|
496
|
-
onProgress
|
|
487
|
+
onProgress,
|
|
488
|
+
parsed
|
|
497
489
|
};
|
|
498
490
|
};
|
|
499
491
|
|
|
@@ -551,15 +543,24 @@ const unsendable = (method, args, error) => {
|
|
|
551
543
|
const cause = {
|
|
552
544
|
cause: error
|
|
553
545
|
};
|
|
554
|
-
return index < 0 ? Object.assign(fail('invalid-argument', `Invalid argument: it cannot be sent to the worker
|
|
546
|
+
return index < 0 ? Object.assign(fail('invalid-argument', `Invalid argument: it cannot be sent to the worker.`), cause) : Object.assign(fail('invalid-argument', `Invalid ${names[index]}: it cannot be sent to the worker.`, names[index]), cause);
|
|
555
547
|
};
|
|
556
548
|
|
|
557
549
|
// Like fetch: an aborted call rejects with the signal's reason, an AbortError unless the caller gave another.
|
|
558
550
|
const abortReason = signal => signal.reason !== undefined ? signal.reason : new DOMException('This operation was aborted', 'AbortError');
|
|
551
|
+
|
|
552
|
+
// The option keys each function takes (the main entry's lists).
|
|
553
|
+
const keysOf = {
|
|
554
|
+
buildClustering: clusterKeys,
|
|
555
|
+
buildPalette: clusterKeys,
|
|
556
|
+
buildSwatches: clusterKeys,
|
|
557
|
+
quantize: quantizeKeys,
|
|
558
|
+
quantizeToIndices: quantizeKeys,
|
|
559
|
+
quantizeWithPalette: mapKeys,
|
|
560
|
+
quantizeToIndicesWithPalette: mapIndexKeys
|
|
561
|
+
};
|
|
559
562
|
const imageFunctions = ['buildClustering', 'buildPalette', 'buildSwatches', 'quantize', 'quantizeWithPalette', 'quantizeToIndices', 'quantizeToIndicesWithPalette'];
|
|
560
563
|
const mappedImageFunctions = ['quantize', 'quantizeWithPalette'];
|
|
561
|
-
// The functions that only cluster, and so take no dither option.
|
|
562
|
-
const clusteringOnlyFunctions = ['buildClustering', 'buildPalette', 'buildSwatches'];
|
|
563
564
|
|
|
564
565
|
// Calls each worker's `abandon` once the worker made by createWorker() has been garbage-collected (see `abandoned`).
|
|
565
566
|
const abandonment = typeof FinalizationRegistry === 'function' ? new FinalizationRegistry(abandon => abandon()) : null;
|
|
@@ -908,11 +909,25 @@ const makeWorker = (options, allowScript) => {
|
|
|
908
909
|
// then the rest of the image's checks (its fields are read once here), before the options, as in the main entry
|
|
909
910
|
const checked = send(image);
|
|
910
911
|
const position = optionsPosition[name];
|
|
912
|
+
// then the argument before the options (k, or a palette), and the options, as the main entry orders them
|
|
913
|
+
const shape = imageShape(checked);
|
|
914
|
+
const byPalette = name.endsWith('WithPalette');
|
|
915
|
+
if (position === 1) {
|
|
916
|
+
if (byPalette) {
|
|
917
|
+
checkPalette(rest[0]);
|
|
918
|
+
} else {
|
|
919
|
+
checkK(rest[0]);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
911
922
|
const {
|
|
912
923
|
options,
|
|
913
924
|
signal,
|
|
914
|
-
onProgress
|
|
915
|
-
|
|
925
|
+
onProgress,
|
|
926
|
+
parsed
|
|
927
|
+
} = takeControls(rest[position], keysOf[name], shape);
|
|
928
|
+
if (byPalette) {
|
|
929
|
+
checkNotEmpty(rest[0], shape, name === 'quantizeToIndicesWithPalette' && parsed.transparent > 0 ? parsed.transparent : 1);
|
|
930
|
+
}
|
|
916
931
|
let args = rest.slice();
|
|
917
932
|
if (position < args.length) {
|
|
918
933
|
args[position] = options;
|
|
@@ -972,7 +987,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
972
987
|
const looksLikeImage = value => value !== null && typeof value === 'object' && (isUint8Array(value) || isUint8ClampedArray(value) || isImageData(value) || 'data' in value);
|
|
973
988
|
// Builds the handle object for what the worker keeps under `id`: `kind` is 'image' (from `cluster`: an image and its
|
|
974
989
|
// clustering) or 'clustering' (from `load`: a clustering alone). `output` turns a mapped result back into the type of the image given.
|
|
975
|
-
const makeHandle = (id, kind, output) => {
|
|
990
|
+
const makeHandle = (id, kind, output, shape) => {
|
|
976
991
|
let released = false;
|
|
977
992
|
// Every method goes through `use`, which refers to `handle`, so a method kept after the handle itself is dropped
|
|
978
993
|
// keeps it alive and it is not reclaimed while still in use.
|
|
@@ -983,13 +998,36 @@ const makeWorker = (options, allowScript) => {
|
|
|
983
998
|
return invoke(method, [id, ...args], [], new Error(), controls);
|
|
984
999
|
};
|
|
985
1000
|
// Calls with mapping options, whose signal and progress callback stay on this side (see takeControls).
|
|
986
|
-
|
|
1001
|
+
// `lead` is what comes first: k or a palette, checked before the options, as in the main entry. `keys` are the options the
|
|
1002
|
+
// method takes, and `shape` the image of this handle (see imageShape).
|
|
1003
|
+
const withControls = (method, leading, mapOptions, keys, lead) => {
|
|
987
1004
|
let taken;
|
|
988
1005
|
try {
|
|
989
|
-
|
|
1006
|
+
if (lead === 'palette') {
|
|
1007
|
+
checkPalette(leading[0]);
|
|
1008
|
+
} else {
|
|
1009
|
+
checkK(leading[0]);
|
|
1010
|
+
}
|
|
1011
|
+
taken = takeControls(mapOptions, keys, shape);
|
|
990
1012
|
} catch (error) {
|
|
991
1013
|
return Promise.reject(error);
|
|
992
1014
|
}
|
|
1015
|
+
// A palette with no colours is wrong only if the image has pixels to map to it, which the thread knows (this side keeps
|
|
1016
|
+
// the image's size only). So with a signal already aborted, the thread checks the call, as the functions do, and the abort
|
|
1017
|
+
// is reported when that finds nothing wrong.
|
|
1018
|
+
if (lead === 'palette' && leading[0].length === 0 && taken.signal && taken.signal.aborted) {
|
|
1019
|
+
const aborted = taken.signal;
|
|
1020
|
+
return use(method, [...leading, taken.options], {
|
|
1021
|
+
check: true
|
|
1022
|
+
}).then(() => {
|
|
1023
|
+
throw abortReason(aborted);
|
|
1024
|
+
}, error => {
|
|
1025
|
+
if (isCodedError(error) && error.argument !== undefined) {
|
|
1026
|
+
throw error;
|
|
1027
|
+
}
|
|
1028
|
+
throw abortReason(aborted);
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
993
1031
|
return use(method, [...leading, taken.options], {
|
|
994
1032
|
signal: taken.signal,
|
|
995
1033
|
onProgress: taken.onProgress
|
|
@@ -997,29 +1035,34 @@ const makeWorker = (options, allowScript) => {
|
|
|
997
1035
|
};
|
|
998
1036
|
// The cuts take no options (the handle's clustering already says how it was made): one passed anyway is an error,
|
|
999
1037
|
// as in the main entry, rather than silently ignored.
|
|
1000
|
-
const plain = (method, name, args, extra) =>
|
|
1038
|
+
const plain = (method, name, args, extra) => {
|
|
1039
|
+
// k first, then the options that aren't taken, as the main entry's cuts check them
|
|
1040
|
+
try {
|
|
1041
|
+
if (args.length > 0) {
|
|
1042
|
+
checkK(args[0]);
|
|
1043
|
+
}
|
|
1044
|
+
} catch (error) {
|
|
1045
|
+
return Promise.reject(error);
|
|
1046
|
+
}
|
|
1047
|
+
return extra.some(value => value !== undefined) ? Promise.reject(fail('invalid-argument', `Invalid arguments: handle.${name} takes no options. To map pixels with options, use handle.quantize.`, 'options')) : use(method, args);
|
|
1048
|
+
};
|
|
1001
1049
|
// An image where k belongs is a mix-up between the two kinds of handle: say so, rather than "k must be an integer".
|
|
1002
1050
|
const imageAsK = (name, k) => looksLikeImage(k) ? Promise.reject(fail('invalid-argument', `Invalid k: this handle holds an image (from worker.cluster) and maps it with ${name}(k); pass k, not an image. To map other images with a stored clustering, use worker.load(clustering).`, 'k')) : null;
|
|
1003
1051
|
const handle = {
|
|
1004
1052
|
getPalette: (k, ...extra) => plain('handleGetPalette', 'getPalette', [k], extra),
|
|
1005
1053
|
getSwatches: (k, ...extra) => plain('handleGetSwatches', 'getSwatches', [k], extra),
|
|
1006
1054
|
getClusteringInfo: (...extra) => plain('handleGetClusteringInfo', 'getClusteringInfo', [], extra),
|
|
1007
|
-
// Takes the signal only: the main entry's chooseFrom takes no progress callback, so one given
|
|
1008
|
-
// the
|
|
1009
|
-
// That relies on the thread injecting a progress poster only for calls that have one (see `optionsIndex` in
|
|
1010
|
-
// worker-thread.js): the null is then the only onProgress it sees, so its own option check rejects it.
|
|
1055
|
+
// Takes the signal only: the main entry's chooseFrom takes no progress callback, so one given is rejected here, by
|
|
1056
|
+
// the same check.
|
|
1011
1057
|
chooseFrom: (palette, k, chooseOptions) => {
|
|
1012
1058
|
let taken;
|
|
1013
1059
|
try {
|
|
1014
|
-
|
|
1060
|
+
checkChoice(palette, k);
|
|
1061
|
+
taken = takeControls(chooseOptions, ['colorSpace', 'signal']);
|
|
1015
1062
|
} catch (error) {
|
|
1016
1063
|
return Promise.reject(error);
|
|
1017
1064
|
}
|
|
1018
|
-
|
|
1019
|
-
...taken.options,
|
|
1020
|
-
onProgress: null
|
|
1021
|
-
} : taken.options;
|
|
1022
|
-
return use('handleChooseFrom', [palette, k, sent], {
|
|
1065
|
+
return use('handleChooseFrom', [palette, k, taken.options], {
|
|
1023
1066
|
signal: taken.signal
|
|
1024
1067
|
});
|
|
1025
1068
|
},
|
|
@@ -1047,15 +1090,15 @@ const makeWorker = (options, allowScript) => {
|
|
|
1047
1090
|
if (kind === 'image') {
|
|
1048
1091
|
Object.assign(handle, {
|
|
1049
1092
|
getClustering: (...extra) => plain('handleGetClustering', 'getClustering', [], extra),
|
|
1050
|
-
quantize: (k, mapOptions) => imageAsK('quantize', k) || (async () => output(await withControls('handleQuantize', [k], mapOptions)))(),
|
|
1051
|
-
quantizeToIndices: (k, mapOptions) => imageAsK('quantizeToIndices', k) || withControls('handleQuantizeToIndices', [k], mapOptions),
|
|
1052
|
-
quantizeWithPalette: async (palette, mapOptions) => output(await withControls('handleQuantizeWithPalette', [palette], mapOptions)),
|
|
1053
|
-
quantizeToIndicesWithPalette: (palette, mapOptions) => withControls('handleQuantizeToIndicesWithPalette', [palette], mapOptions)
|
|
1093
|
+
quantize: (k, mapOptions) => imageAsK('quantize', k) || (async () => output(await withControls('handleQuantize', [k], mapOptions, mapKeys, 'k')))(),
|
|
1094
|
+
quantizeToIndices: (k, mapOptions) => imageAsK('quantizeToIndices', k) || withControls('handleQuantizeToIndices', [k], mapOptions, mapIndexKeys, 'k'),
|
|
1095
|
+
quantizeWithPalette: async (palette, mapOptions) => output(await withControls('handleQuantizeWithPalette', [palette], mapOptions, mapKeys, 'palette')),
|
|
1096
|
+
quantizeToIndicesWithPalette: (palette, mapOptions) => withControls('handleQuantizeToIndicesWithPalette', [palette], mapOptions, mapIndexKeys, 'palette')
|
|
1054
1097
|
});
|
|
1055
1098
|
}
|
|
1056
1099
|
if (kind === 'clustering') {
|
|
1057
1100
|
// The handle holds no image: each call maps the image it is given, and returns the type of that image.
|
|
1058
|
-
const mapping = (name, method, mapped) => (image, k, mapOptions) => {
|
|
1101
|
+
const mapping = (name, method, mapped, keys) => (image, k, mapOptions) => {
|
|
1059
1102
|
if (typeof image === 'number') {
|
|
1060
1103
|
return Promise.reject(fail('invalid-argument', "Invalid image: this handle holds a clustering, not an image (from worker.load); pass the image, then k.", 'image'));
|
|
1061
1104
|
}
|
|
@@ -1069,11 +1112,12 @@ const makeWorker = (options, allowScript) => {
|
|
|
1069
1112
|
throw bare;
|
|
1070
1113
|
}
|
|
1071
1114
|
const checked = send(image);
|
|
1115
|
+
checkK(k);
|
|
1072
1116
|
const {
|
|
1073
1117
|
options,
|
|
1074
1118
|
signal,
|
|
1075
1119
|
onProgress
|
|
1076
|
-
} = takeControls(mapOptions);
|
|
1120
|
+
} = takeControls(mapOptions, keys, imageShape(checked));
|
|
1077
1121
|
if (signal && signal.aborted) {
|
|
1078
1122
|
// As the functions on the worker do: a wrong argument is reported before the abort.
|
|
1079
1123
|
try {
|
|
@@ -1100,8 +1144,8 @@ const makeWorker = (options, allowScript) => {
|
|
|
1100
1144
|
})();
|
|
1101
1145
|
};
|
|
1102
1146
|
Object.assign(handle, {
|
|
1103
|
-
quantize: mapping('quantize', 'handleQuantizeImage', true),
|
|
1104
|
-
quantizeToIndices: mapping('quantizeToIndices', 'handleQuantizeToIndicesImage', false)
|
|
1147
|
+
quantize: mapping('quantize', 'handleQuantizeImage', true, mapKeys),
|
|
1148
|
+
quantizeToIndices: mapping('quantizeToIndices', 'handleQuantizeToIndicesImage', false, mapIndexKeys)
|
|
1105
1149
|
});
|
|
1106
1150
|
}
|
|
1107
1151
|
// `await using handle = await worker.cluster(image)` releases it at the end of the block.
|
|
@@ -1115,7 +1159,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
1115
1159
|
};
|
|
1116
1160
|
|
|
1117
1161
|
// Clusters `image` and keeps it, with its clustering, in the worker.
|
|
1118
|
-
instance.cluster = (image,
|
|
1162
|
+
instance.cluster = (image, givenOptions) => {
|
|
1119
1163
|
const origin = new Error();
|
|
1120
1164
|
return (async () => {
|
|
1121
1165
|
// The image is checked before the options and the abort, as the main entry does.
|
|
@@ -1124,11 +1168,12 @@ const makeWorker = (options, allowScript) => {
|
|
|
1124
1168
|
throw bare;
|
|
1125
1169
|
}
|
|
1126
1170
|
const checked = send(image);
|
|
1171
|
+
const shape = imageShape(checked);
|
|
1127
1172
|
const {
|
|
1128
1173
|
options,
|
|
1129
1174
|
signal,
|
|
1130
1175
|
onProgress
|
|
1131
|
-
} = takeControls(
|
|
1176
|
+
} = takeControls(givenOptions, clusterKeys, shape);
|
|
1132
1177
|
if (signal && signal.aborted) {
|
|
1133
1178
|
throw abortReason(signal);
|
|
1134
1179
|
}
|
|
@@ -1152,7 +1197,13 @@ const makeWorker = (options, allowScript) => {
|
|
|
1152
1197
|
}
|
|
1153
1198
|
throw error;
|
|
1154
1199
|
}
|
|
1155
|
-
|
|
1200
|
+
// The handle keeps the image's size, not its bytes (a transferred buffer is empty by now).
|
|
1201
|
+
return makeHandle(id, 'image', rebuild(image), {
|
|
1202
|
+
...shape,
|
|
1203
|
+
data: {
|
|
1204
|
+
length: shape.data.length
|
|
1205
|
+
}
|
|
1206
|
+
});
|
|
1156
1207
|
})();
|
|
1157
1208
|
};
|
|
1158
1209
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agglomerative-clustering",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.4",
|
|
4
4
|
"description": "Colour palette extraction and image quantization with exact Ward clustering, in WebAssembly. Cluster an image once, then cut it to a palette of any size.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Rohan Fredriksson",
|