agglomerative-clustering 3.2.0 → 3.4.0
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 +28 -21
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +41 -17
- package/dist/index.d.cts +30 -8
- package/dist/index.d.ts +30 -8
- package/dist/index.mjs +38 -15
- package/dist/shared.cjs +103 -9
- package/dist/shared.mjs +101 -8
- package/dist/worker-thread.mjs +21 -5
- package/dist/worker.cjs +82 -26
- package/dist/worker.d.cts +22 -4
- package/dist/worker.d.ts +22 -4
- package/dist/worker.mjs +79 -24
- package/package.json +1 -1
package/dist/worker.cjs
CHANGED
|
@@ -23,6 +23,7 @@ Object.defineProperty(exports, "alphaModes", {
|
|
|
23
23
|
return _shared.alphaModes;
|
|
24
24
|
}
|
|
25
25
|
});
|
|
26
|
+
exports.buildSwatches = exports.buildPalette = exports.buildClustering = void 0;
|
|
26
27
|
Object.defineProperty(exports, "colorSpaces", {
|
|
27
28
|
enumerable: true,
|
|
28
29
|
get: function () {
|
|
@@ -491,15 +492,46 @@ const takeControls = (options, progressAllowed = true, ditherAllowed = true) =>
|
|
|
491
492
|
if (options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
|
|
492
493
|
// One read of each setting (a getter could answer differently twice): the copy is what is checked and sent.
|
|
493
494
|
const copy = (0, _shared.isOptionsObject)(options.dither) ? Object.assign({}, (0, _shared.snapshot)(options.dither)) : options.dither;
|
|
494
|
-
|
|
495
|
+
// A wrong setting is the worker's to report, after the image and k, as the main entry orders its checks. Only a
|
|
496
|
+
// copy that can't be sent is reported here.
|
|
497
|
+
try {
|
|
498
|
+
(0, _shared.parseDither)(copy);
|
|
499
|
+
} catch (error) {
|
|
500
|
+
if (!sendable(copy)) {
|
|
501
|
+
throw error;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
495
504
|
options.dither = copy;
|
|
496
505
|
}
|
|
506
|
+
// The signal and the progress callback can't cross to the worker, so they are kept here and the worker gets its own.
|
|
507
|
+
// A wrong one (or a callback where only a signal is taken) crosses as `null` instead, which the worker's validation
|
|
508
|
+
// rejects with the main entry's error, after the image and k: the main entry checks the options last. (This relies on
|
|
509
|
+
// those messages not echoing the value: if they ever say what they got, send a stand-in that reads the same.)
|
|
497
510
|
const {
|
|
498
511
|
signal,
|
|
499
512
|
onProgress
|
|
500
513
|
} = options;
|
|
501
|
-
|
|
502
|
-
|
|
514
|
+
const badSignal = signal !== undefined && (signal === null || typeof signal !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function');
|
|
515
|
+
const badProgress = onProgress !== undefined && (!progressAllowed || typeof onProgress !== 'function');
|
|
516
|
+
const {
|
|
517
|
+
signal: _signal,
|
|
518
|
+
onProgress: _onProgress,
|
|
519
|
+
...rest
|
|
520
|
+
} = options;
|
|
521
|
+
if (badSignal || badProgress) {
|
|
522
|
+
return {
|
|
523
|
+
options: {
|
|
524
|
+
...rest,
|
|
525
|
+
...(badSignal ? {
|
|
526
|
+
signal: null
|
|
527
|
+
} : {}),
|
|
528
|
+
...(badProgress ? {
|
|
529
|
+
onProgress: null
|
|
530
|
+
} : {})
|
|
531
|
+
},
|
|
532
|
+
signal: undefined,
|
|
533
|
+
onProgress: undefined
|
|
534
|
+
};
|
|
503
535
|
}
|
|
504
536
|
if (signal === undefined && onProgress === undefined) {
|
|
505
537
|
return {
|
|
@@ -508,17 +540,6 @@ const takeControls = (options, progressAllowed = true, ditherAllowed = true) =>
|
|
|
508
540
|
onProgress
|
|
509
541
|
};
|
|
510
542
|
}
|
|
511
|
-
if (signal !== undefined && (signal === null || typeof signal !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function')) {
|
|
512
|
-
throw (0, _shared.fail)('invalid-argument', "Invalid signal: must be an AbortSignal.", 'options.signal');
|
|
513
|
-
}
|
|
514
|
-
if (onProgress !== undefined && typeof onProgress !== 'function') {
|
|
515
|
-
throw (0, _shared.fail)('invalid-argument', "Invalid onProgress: must be a function.", 'options.onProgress');
|
|
516
|
-
}
|
|
517
|
-
const {
|
|
518
|
-
signal: _signal,
|
|
519
|
-
onProgress: _onProgress,
|
|
520
|
-
...rest
|
|
521
|
-
} = options;
|
|
522
543
|
return {
|
|
523
544
|
options: rest,
|
|
524
545
|
signal,
|
|
@@ -526,6 +547,19 @@ const takeControls = (options, progressAllowed = true, ditherAllowed = true) =>
|
|
|
526
547
|
};
|
|
527
548
|
};
|
|
528
549
|
|
|
550
|
+
// Whether a value survives being posted to the worker.
|
|
551
|
+
const sendable = value => {
|
|
552
|
+
if (typeof structuredClone !== 'function') {
|
|
553
|
+
return true;
|
|
554
|
+
}
|
|
555
|
+
try {
|
|
556
|
+
structuredClone(value);
|
|
557
|
+
return true;
|
|
558
|
+
} catch (error) {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
|
|
529
563
|
// Where the options sit among a function's arguments after the image (see takeControls).
|
|
530
564
|
const optionsPosition = {
|
|
531
565
|
getClustering: 0,
|
|
@@ -764,7 +798,8 @@ const makeWorker = (options, allowScript) => {
|
|
|
764
798
|
};
|
|
765
799
|
const post = (method, args, transferable, origin, {
|
|
766
800
|
signal,
|
|
767
|
-
onProgress
|
|
801
|
+
onProgress,
|
|
802
|
+
check
|
|
768
803
|
} = {}) => {
|
|
769
804
|
if (terminated) {
|
|
770
805
|
return Promise.reject((0, _shared.fail)('worker-terminated', "Worker terminated."));
|
|
@@ -813,7 +848,10 @@ const makeWorker = (options, allowScript) => {
|
|
|
813
848
|
method,
|
|
814
849
|
args: args.map(_shared.mark),
|
|
815
850
|
cancellable: signal !== undefined || onProgress !== undefined,
|
|
816
|
-
progress: onProgress !== undefined
|
|
851
|
+
progress: onProgress !== undefined,
|
|
852
|
+
...(check ? {
|
|
853
|
+
check
|
|
854
|
+
} : {})
|
|
817
855
|
};
|
|
818
856
|
try {
|
|
819
857
|
connection.post(message, transfer ? transferable : []);
|
|
@@ -851,14 +889,24 @@ const makeWorker = (options, allowScript) => {
|
|
|
851
889
|
signal,
|
|
852
890
|
onProgress
|
|
853
891
|
} = takeControls(rest[position], !mappingOnlyFunctions.includes(name), !clusteringOnlyFunctions.includes(name));
|
|
854
|
-
// Checked before the image is copied or transferred away, so an aborted call leaves it with the caller.
|
|
855
|
-
if (signal && signal.aborted) {
|
|
856
|
-
throw abortReason(signal);
|
|
857
|
-
}
|
|
858
892
|
let args = rest.slice();
|
|
859
893
|
if (position < args.length) {
|
|
860
894
|
args[position] = options;
|
|
861
895
|
}
|
|
896
|
+
if (signal && signal.aborted) {
|
|
897
|
+
// The main entry reports a wrong argument before an abort, so the worker checks the arguments (a
|
|
898
|
+
// copy, never transferred: an aborted call leaves the image with the caller) without doing the work.
|
|
899
|
+
try {
|
|
900
|
+
await invoke(name, [own(send(image)), ...args.map(own)], [], origin, {
|
|
901
|
+
check: true
|
|
902
|
+
});
|
|
903
|
+
} catch (error) {
|
|
904
|
+
if ((0, _shared.isCodedError)(error) && error.argument !== undefined) {
|
|
905
|
+
throw error;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
throw abortReason(signal);
|
|
909
|
+
}
|
|
862
910
|
let sent = send(image);
|
|
863
911
|
if (!ready()) {
|
|
864
912
|
sent = take(sent);
|
|
@@ -875,6 +923,11 @@ const makeWorker = (options, allowScript) => {
|
|
|
875
923
|
for (const name of otherFunctions) {
|
|
876
924
|
instance[name] = (...args) => invoke(name, ready() ? args : args.map(own), [], new Error());
|
|
877
925
|
}
|
|
926
|
+
// 3.4.0's names for the functions that cluster an image. The old names are the same functions, and they still name the
|
|
927
|
+
// call on the wire, so a worker script copied from an older version still understands it.
|
|
928
|
+
instance.buildClustering = instance.getClustering;
|
|
929
|
+
instance.buildPalette = instance.getPalette;
|
|
930
|
+
instance.buildSwatches = instance.getSwatches;
|
|
878
931
|
instance.init = initArgs => {
|
|
879
932
|
const origin = new Error();
|
|
880
933
|
let checked;
|
|
@@ -1052,14 +1105,17 @@ const terminate = async () => {
|
|
|
1052
1105
|
exports.terminate = terminate;
|
|
1053
1106
|
const init = (...args) => sharedWorker().init(...args);
|
|
1054
1107
|
exports.init = init;
|
|
1055
|
-
const
|
|
1056
|
-
exports.
|
|
1057
|
-
const
|
|
1058
|
-
|
|
1108
|
+
const buildClustering = (...args) => sharedWorker().buildClustering(...args);
|
|
1109
|
+
exports.buildClustering = buildClustering;
|
|
1110
|
+
const getClustering = exports.getClustering = buildClustering;
|
|
1111
|
+
const buildPalette = (...args) => sharedWorker().buildPalette(...args);
|
|
1112
|
+
exports.buildPalette = buildPalette;
|
|
1113
|
+
const getPalette = exports.getPalette = buildPalette;
|
|
1059
1114
|
const getPaletteFromClustering = (...args) => sharedWorker().getPaletteFromClustering(...args);
|
|
1060
1115
|
exports.getPaletteFromClustering = getPaletteFromClustering;
|
|
1061
|
-
const
|
|
1062
|
-
exports.
|
|
1116
|
+
const buildSwatches = (...args) => sharedWorker().buildSwatches(...args);
|
|
1117
|
+
exports.buildSwatches = buildSwatches;
|
|
1118
|
+
const getSwatches = exports.getSwatches = buildSwatches;
|
|
1063
1119
|
const getSwatchesFromClustering = (...args) => sharedWorker().getSwatchesFromClustering(...args);
|
|
1064
1120
|
exports.getSwatchesFromClustering = getSwatchesFromClustering;
|
|
1065
1121
|
const getClusteringInfo = (...args) => sharedWorker().getClusteringInfo(...args);
|
package/dist/worker.d.cts
CHANGED
|
@@ -11,7 +11,10 @@ import type { Palette, Clustering } from './index.cjs';
|
|
|
11
11
|
/** The same class as the main entry's `Palette`: flat RGB bytes that know their colour space. */
|
|
12
12
|
export { Palette } from './index.cjs';
|
|
13
13
|
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* The same class as the main entry's `Clustering`. Its methods run on the calling thread and need the main entry imported
|
|
16
|
+
* there; off the thread, use this entry's `…FromClustering` functions or `cluster()`.
|
|
17
|
+
*/
|
|
15
18
|
export { Clustering } from './index.cjs';
|
|
16
19
|
|
|
17
20
|
/** The same list as the main entry's `errorCodes`: the worker entry's errors have the same codes. */
|
|
@@ -89,9 +92,18 @@ export interface ClusteringHandle<Image extends ImageLike = ImageLike> {
|
|
|
89
92
|
export interface AgglomerativeWorker {
|
|
90
93
|
/** Loads the module in the worker. `wasm` is the only option, and `createWorker({ wasm })` does the same. */
|
|
91
94
|
init(options?: InitOptions): Promise<void>;
|
|
95
|
+
/** The main entry's `buildClustering`, run in this worker. */
|
|
96
|
+
buildClustering(image: ImageLike, options?: ClusteringOptions): Promise<Clustering>;
|
|
97
|
+
/** @deprecated Renamed {@link AgglomerativeWorker.buildClustering} in 3.4.0 (the same function). Removed in 4.0. */
|
|
92
98
|
getClustering(image: ImageLike, options?: ClusteringOptions): Promise<Clustering>;
|
|
99
|
+
/** The main entry's `buildPalette`, run in this worker. */
|
|
100
|
+
buildPalette(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Palette>;
|
|
101
|
+
/** @deprecated Renamed {@link AgglomerativeWorker.buildPalette} in 3.4.0 (the same function). Removed in 4.0. */
|
|
93
102
|
getPalette(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Palette>;
|
|
94
103
|
getPaletteFromClustering(clustering: Clustering, k: number): Promise<Palette>;
|
|
104
|
+
/** The main entry's `buildSwatches`, run in this worker. */
|
|
105
|
+
buildSwatches(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Swatches>;
|
|
106
|
+
/** @deprecated Renamed {@link AgglomerativeWorker.buildSwatches} in 3.4.0 (the same function). Removed in 4.0. */
|
|
95
107
|
getSwatches(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Swatches>;
|
|
96
108
|
getSwatchesFromClustering(clustering: Clustering, k: number): Promise<Swatches>;
|
|
97
109
|
getClusteringInfo(clustering: Clustering): Promise<ClusteringInfo>;
|
|
@@ -126,13 +138,19 @@ export function terminate(): Promise<void>;
|
|
|
126
138
|
* shared worker's `script`, before any other call.
|
|
127
139
|
*/
|
|
128
140
|
export function init(options?: SharedInitOptions): Promise<void>;
|
|
129
|
-
/** The main entry's `
|
|
141
|
+
/** The main entry's `buildClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
142
|
+
export const buildClustering: AgglomerativeWorker['buildClustering'];
|
|
143
|
+
/** @deprecated Renamed {@link buildClustering} in 3.4.0 (the same function). Removed in 4.0. */
|
|
130
144
|
export const getClustering: AgglomerativeWorker['getClustering'];
|
|
131
|
-
/** The main entry's `
|
|
145
|
+
/** The main entry's `buildPalette`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
146
|
+
export const buildPalette: AgglomerativeWorker['buildPalette'];
|
|
147
|
+
/** @deprecated Renamed {@link buildPalette} in 3.4.0 (the same function). Removed in 4.0. */
|
|
132
148
|
export const getPalette: AgglomerativeWorker['getPalette'];
|
|
133
149
|
/** The main entry's `getPaletteFromClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
134
150
|
export const getPaletteFromClustering: AgglomerativeWorker['getPaletteFromClustering'];
|
|
135
|
-
/** The main entry's `
|
|
151
|
+
/** The main entry's `buildSwatches`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
152
|
+
export const buildSwatches: AgglomerativeWorker['buildSwatches'];
|
|
153
|
+
/** @deprecated Renamed {@link buildSwatches} in 3.4.0 (the same function). Removed in 4.0. */
|
|
136
154
|
export const getSwatches: AgglomerativeWorker['getSwatches'];
|
|
137
155
|
/** The main entry's `getSwatchesFromClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
138
156
|
export const getSwatchesFromClustering: AgglomerativeWorker['getSwatchesFromClustering'];
|
package/dist/worker.d.ts
CHANGED
|
@@ -11,7 +11,10 @@ import type { Palette, Clustering } from './index.js';
|
|
|
11
11
|
/** The same class as the main entry's `Palette`: flat RGB bytes that know their colour space. */
|
|
12
12
|
export { Palette } from './index.js';
|
|
13
13
|
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* The same class as the main entry's `Clustering`. Its methods run on the calling thread and need the main entry imported
|
|
16
|
+
* there; off the thread, use this entry's `…FromClustering` functions or `cluster()`.
|
|
17
|
+
*/
|
|
15
18
|
export { Clustering } from './index.js';
|
|
16
19
|
|
|
17
20
|
/** The same list as the main entry's `errorCodes`: the worker entry's errors have the same codes. */
|
|
@@ -89,9 +92,18 @@ export interface ClusteringHandle<Image extends ImageLike = ImageLike> {
|
|
|
89
92
|
export interface AgglomerativeWorker {
|
|
90
93
|
/** Loads the module in the worker. `wasm` is the only option, and `createWorker({ wasm })` does the same. */
|
|
91
94
|
init(options?: InitOptions): Promise<void>;
|
|
95
|
+
/** The main entry's `buildClustering`, run in this worker. */
|
|
96
|
+
buildClustering(image: ImageLike, options?: ClusteringOptions): Promise<Clustering>;
|
|
97
|
+
/** @deprecated Renamed {@link AgglomerativeWorker.buildClustering} in 3.4.0 (the same function). Removed in 4.0. */
|
|
92
98
|
getClustering(image: ImageLike, options?: ClusteringOptions): Promise<Clustering>;
|
|
99
|
+
/** The main entry's `buildPalette`, run in this worker. */
|
|
100
|
+
buildPalette(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Palette>;
|
|
101
|
+
/** @deprecated Renamed {@link AgglomerativeWorker.buildPalette} in 3.4.0 (the same function). Removed in 4.0. */
|
|
93
102
|
getPalette(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Palette>;
|
|
94
103
|
getPaletteFromClustering(clustering: Clustering, k: number): Promise<Palette>;
|
|
104
|
+
/** The main entry's `buildSwatches`, run in this worker. */
|
|
105
|
+
buildSwatches(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Swatches>;
|
|
106
|
+
/** @deprecated Renamed {@link AgglomerativeWorker.buildSwatches} in 3.4.0 (the same function). Removed in 4.0. */
|
|
95
107
|
getSwatches(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Swatches>;
|
|
96
108
|
getSwatchesFromClustering(clustering: Clustering, k: number): Promise<Swatches>;
|
|
97
109
|
getClusteringInfo(clustering: Clustering): Promise<ClusteringInfo>;
|
|
@@ -126,13 +138,19 @@ export function terminate(): Promise<void>;
|
|
|
126
138
|
* shared worker's `script`, before any other call.
|
|
127
139
|
*/
|
|
128
140
|
export function init(options?: SharedInitOptions): Promise<void>;
|
|
129
|
-
/** The main entry's `
|
|
141
|
+
/** The main entry's `buildClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
142
|
+
export const buildClustering: AgglomerativeWorker['buildClustering'];
|
|
143
|
+
/** @deprecated Renamed {@link buildClustering} in 3.4.0 (the same function). Removed in 4.0. */
|
|
130
144
|
export const getClustering: AgglomerativeWorker['getClustering'];
|
|
131
|
-
/** The main entry's `
|
|
145
|
+
/** The main entry's `buildPalette`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
146
|
+
export const buildPalette: AgglomerativeWorker['buildPalette'];
|
|
147
|
+
/** @deprecated Renamed {@link buildPalette} in 3.4.0 (the same function). Removed in 4.0. */
|
|
132
148
|
export const getPalette: AgglomerativeWorker['getPalette'];
|
|
133
149
|
/** The main entry's `getPaletteFromClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
134
150
|
export const getPaletteFromClustering: AgglomerativeWorker['getPaletteFromClustering'];
|
|
135
|
-
/** The main entry's `
|
|
151
|
+
/** The main entry's `buildSwatches`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
152
|
+
export const buildSwatches: AgglomerativeWorker['buildSwatches'];
|
|
153
|
+
/** @deprecated Renamed {@link buildSwatches} in 3.4.0 (the same function). Removed in 4.0. */
|
|
136
154
|
export const getSwatches: AgglomerativeWorker['getSwatches'];
|
|
137
155
|
/** The main entry's `getSwatchesFromClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
138
156
|
export const getSwatchesFromClustering: AgglomerativeWorker['getSwatchesFromClustering'];
|
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 { fail, detachedError, isOptionsObject, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge } from "./shared.mjs";
|
|
7
|
+
import { fail, isCodedError, detachedError, isOptionsObject, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge } 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";
|
|
@@ -423,15 +423,46 @@ const takeControls = (options, progressAllowed = true, ditherAllowed = true) =>
|
|
|
423
423
|
if (options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
|
|
424
424
|
// One read of each setting (a getter could answer differently twice): the copy is what is checked and sent.
|
|
425
425
|
const copy = isOptionsObject(options.dither) ? Object.assign({}, snapshot(options.dither)) : options.dither;
|
|
426
|
-
|
|
426
|
+
// A wrong setting is the worker's to report, after the image and k, as the main entry orders its checks. Only a
|
|
427
|
+
// copy that can't be sent is reported here.
|
|
428
|
+
try {
|
|
429
|
+
parseDither(copy);
|
|
430
|
+
} catch (error) {
|
|
431
|
+
if (!sendable(copy)) {
|
|
432
|
+
throw error;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
427
435
|
options.dither = copy;
|
|
428
436
|
}
|
|
437
|
+
// The signal and the progress callback can't cross to the worker, so they are kept here and the worker gets its own.
|
|
438
|
+
// A wrong one (or a callback where only a signal is taken) crosses as `null` instead, which the worker's validation
|
|
439
|
+
// rejects with the main entry's error, after the image and k: the main entry checks the options last. (This relies on
|
|
440
|
+
// those messages not echoing the value: if they ever say what they got, send a stand-in that reads the same.)
|
|
429
441
|
const {
|
|
430
442
|
signal,
|
|
431
443
|
onProgress
|
|
432
444
|
} = options;
|
|
433
|
-
|
|
434
|
-
|
|
445
|
+
const badSignal = signal !== undefined && (signal === null || typeof signal !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function');
|
|
446
|
+
const badProgress = onProgress !== undefined && (!progressAllowed || typeof onProgress !== 'function');
|
|
447
|
+
const {
|
|
448
|
+
signal: _signal,
|
|
449
|
+
onProgress: _onProgress,
|
|
450
|
+
...rest
|
|
451
|
+
} = options;
|
|
452
|
+
if (badSignal || badProgress) {
|
|
453
|
+
return {
|
|
454
|
+
options: {
|
|
455
|
+
...rest,
|
|
456
|
+
...(badSignal ? {
|
|
457
|
+
signal: null
|
|
458
|
+
} : {}),
|
|
459
|
+
...(badProgress ? {
|
|
460
|
+
onProgress: null
|
|
461
|
+
} : {})
|
|
462
|
+
},
|
|
463
|
+
signal: undefined,
|
|
464
|
+
onProgress: undefined
|
|
465
|
+
};
|
|
435
466
|
}
|
|
436
467
|
if (signal === undefined && onProgress === undefined) {
|
|
437
468
|
return {
|
|
@@ -440,17 +471,6 @@ const takeControls = (options, progressAllowed = true, ditherAllowed = true) =>
|
|
|
440
471
|
onProgress
|
|
441
472
|
};
|
|
442
473
|
}
|
|
443
|
-
if (signal !== undefined && (signal === null || typeof signal !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function')) {
|
|
444
|
-
throw fail('invalid-argument', "Invalid signal: must be an AbortSignal.", 'options.signal');
|
|
445
|
-
}
|
|
446
|
-
if (onProgress !== undefined && typeof onProgress !== 'function') {
|
|
447
|
-
throw fail('invalid-argument', "Invalid onProgress: must be a function.", 'options.onProgress');
|
|
448
|
-
}
|
|
449
|
-
const {
|
|
450
|
-
signal: _signal,
|
|
451
|
-
onProgress: _onProgress,
|
|
452
|
-
...rest
|
|
453
|
-
} = options;
|
|
454
474
|
return {
|
|
455
475
|
options: rest,
|
|
456
476
|
signal,
|
|
@@ -458,6 +478,19 @@ const takeControls = (options, progressAllowed = true, ditherAllowed = true) =>
|
|
|
458
478
|
};
|
|
459
479
|
};
|
|
460
480
|
|
|
481
|
+
// Whether a value survives being posted to the worker.
|
|
482
|
+
const sendable = value => {
|
|
483
|
+
if (typeof structuredClone !== 'function') {
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
try {
|
|
487
|
+
structuredClone(value);
|
|
488
|
+
return true;
|
|
489
|
+
} catch (error) {
|
|
490
|
+
return false;
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
|
|
461
494
|
// Where the options sit among a function's arguments after the image (see takeControls).
|
|
462
495
|
const optionsPosition = {
|
|
463
496
|
getClustering: 0,
|
|
@@ -696,7 +729,8 @@ const makeWorker = (options, allowScript) => {
|
|
|
696
729
|
};
|
|
697
730
|
const post = (method, args, transferable, origin, {
|
|
698
731
|
signal,
|
|
699
|
-
onProgress
|
|
732
|
+
onProgress,
|
|
733
|
+
check
|
|
700
734
|
} = {}) => {
|
|
701
735
|
if (terminated) {
|
|
702
736
|
return Promise.reject(fail('worker-terminated', "Worker terminated."));
|
|
@@ -745,7 +779,10 @@ const makeWorker = (options, allowScript) => {
|
|
|
745
779
|
method,
|
|
746
780
|
args: args.map(mark),
|
|
747
781
|
cancellable: signal !== undefined || onProgress !== undefined,
|
|
748
|
-
progress: onProgress !== undefined
|
|
782
|
+
progress: onProgress !== undefined,
|
|
783
|
+
...(check ? {
|
|
784
|
+
check
|
|
785
|
+
} : {})
|
|
749
786
|
};
|
|
750
787
|
try {
|
|
751
788
|
connection.post(message, transfer ? transferable : []);
|
|
@@ -783,14 +820,24 @@ const makeWorker = (options, allowScript) => {
|
|
|
783
820
|
signal,
|
|
784
821
|
onProgress
|
|
785
822
|
} = takeControls(rest[position], !mappingOnlyFunctions.includes(name), !clusteringOnlyFunctions.includes(name));
|
|
786
|
-
// Checked before the image is copied or transferred away, so an aborted call leaves it with the caller.
|
|
787
|
-
if (signal && signal.aborted) {
|
|
788
|
-
throw abortReason(signal);
|
|
789
|
-
}
|
|
790
823
|
let args = rest.slice();
|
|
791
824
|
if (position < args.length) {
|
|
792
825
|
args[position] = options;
|
|
793
826
|
}
|
|
827
|
+
if (signal && signal.aborted) {
|
|
828
|
+
// The main entry reports a wrong argument before an abort, so the worker checks the arguments (a
|
|
829
|
+
// copy, never transferred: an aborted call leaves the image with the caller) without doing the work.
|
|
830
|
+
try {
|
|
831
|
+
await invoke(name, [own(send(image)), ...args.map(own)], [], origin, {
|
|
832
|
+
check: true
|
|
833
|
+
});
|
|
834
|
+
} catch (error) {
|
|
835
|
+
if (isCodedError(error) && error.argument !== undefined) {
|
|
836
|
+
throw error;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
throw abortReason(signal);
|
|
840
|
+
}
|
|
794
841
|
let sent = send(image);
|
|
795
842
|
if (!ready()) {
|
|
796
843
|
sent = take(sent);
|
|
@@ -807,6 +854,11 @@ const makeWorker = (options, allowScript) => {
|
|
|
807
854
|
for (const name of otherFunctions) {
|
|
808
855
|
instance[name] = (...args) => invoke(name, ready() ? args : args.map(own), [], new Error());
|
|
809
856
|
}
|
|
857
|
+
// 3.4.0's names for the functions that cluster an image. The old names are the same functions, and they still name the
|
|
858
|
+
// call on the wire, so a worker script copied from an older version still understands it.
|
|
859
|
+
instance.buildClustering = instance.getClustering;
|
|
860
|
+
instance.buildPalette = instance.getPalette;
|
|
861
|
+
instance.buildSwatches = instance.getSwatches;
|
|
810
862
|
instance.init = initArgs => {
|
|
811
863
|
const origin = new Error();
|
|
812
864
|
let checked;
|
|
@@ -981,10 +1033,13 @@ export const terminate = async () => {
|
|
|
981
1033
|
await stopping.terminate();
|
|
982
1034
|
};
|
|
983
1035
|
export const init = (...args) => sharedWorker().init(...args);
|
|
984
|
-
export const
|
|
985
|
-
export const
|
|
1036
|
+
export const buildClustering = (...args) => sharedWorker().buildClustering(...args);
|
|
1037
|
+
export const getClustering = buildClustering;
|
|
1038
|
+
export const buildPalette = (...args) => sharedWorker().buildPalette(...args);
|
|
1039
|
+
export const getPalette = buildPalette;
|
|
986
1040
|
export const getPaletteFromClustering = (...args) => sharedWorker().getPaletteFromClustering(...args);
|
|
987
|
-
export const
|
|
1041
|
+
export const buildSwatches = (...args) => sharedWorker().buildSwatches(...args);
|
|
1042
|
+
export const getSwatches = buildSwatches;
|
|
988
1043
|
export const getSwatchesFromClustering = (...args) => sharedWorker().getSwatchesFromClustering(...args);
|
|
989
1044
|
export const getClusteringInfo = (...args) => sharedWorker().getClusteringInfo(...args);
|
|
990
1045
|
export const quantize = (...args) => sharedWorker().quantize(...args);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agglomerative-clustering",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
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",
|