agglomerative-clustering 4.0.0-beta.2 → 4.0.0-beta.3
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 +5 -5
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +26 -56
- package/dist/index.d.cts +15 -76
- package/dist/index.d.ts +15 -76
- package/dist/index.mjs +25 -50
- package/dist/shared.cjs +30 -78
- package/dist/shared.mjs +28 -73
- package/dist/worker-thread.mjs +15 -18
- package/dist/worker.cjs +108 -60
- package/dist/worker.d.cts +0 -32
- package/dist/worker.d.ts +0 -32
- package/dist/worker.mjs +107 -54
- package/package.json +2 -2
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, isCodedError, detachedError, isOptionsObject, isUint8Array, isUint8ClampedArray, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge,
|
|
7
|
+
import { version, fail, isCodedError, detachedError, isOptionsObject, isUint8Array, isUint8ClampedArray, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge, bareImageError, readHeader, checkImageBody, imageCopy } 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";
|
|
@@ -512,24 +512,54 @@ const sendable = value => {
|
|
|
512
512
|
|
|
513
513
|
// Where the options sit among a function's arguments after the image (see takeControls).
|
|
514
514
|
const optionsPosition = {
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
515
|
+
buildClustering: 0,
|
|
516
|
+
buildPalette: 1,
|
|
517
|
+
buildSwatches: 1,
|
|
518
518
|
quantize: 1,
|
|
519
|
-
quantizeFromClustering: 2,
|
|
520
519
|
quantizeWithPalette: 1,
|
|
521
520
|
quantizeToIndices: 1,
|
|
522
|
-
quantizeToIndicesFromClustering: 2,
|
|
523
521
|
quantizeToIndicesWithPalette: 1
|
|
524
522
|
};
|
|
525
523
|
|
|
524
|
+
// The names of the arguments each call posts, for saying which one can't be sent.
|
|
525
|
+
const argumentNames = {
|
|
526
|
+
buildClustering: ['image', 'options'],
|
|
527
|
+
buildPalette: ['image', 'k', 'options'],
|
|
528
|
+
buildSwatches: ['image', 'k', 'options'],
|
|
529
|
+
quantize: ['image', 'k', 'options'],
|
|
530
|
+
quantizeToIndices: ['image', 'k', 'options'],
|
|
531
|
+
quantizeWithPalette: ['image', 'palette', 'options'],
|
|
532
|
+
quantizeToIndicesWithPalette: ['image', 'palette', 'options'],
|
|
533
|
+
init: ['options'],
|
|
534
|
+
cluster: [null, 'image', 'options'],
|
|
535
|
+
load: [null, 'clustering'],
|
|
536
|
+
handleGetPalette: [null, 'k'],
|
|
537
|
+
handleGetSwatches: [null, 'k'],
|
|
538
|
+
handleChooseFrom: [null, 'palette', 'k', 'options'],
|
|
539
|
+
handleQuantize: [null, 'k', 'options'],
|
|
540
|
+
handleQuantizeToIndices: [null, 'k', 'options'],
|
|
541
|
+
handleQuantizeWithPalette: [null, 'palette', 'options'],
|
|
542
|
+
handleQuantizeToIndicesWithPalette: [null, 'palette', 'options'],
|
|
543
|
+
handleQuantizeImage: [null, 'image', 'k', 'options'],
|
|
544
|
+
handleQuantizeToIndicesImage: [null, 'image', 'k', 'options']
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
// The error for a call whose message can't be cloned: the first argument that fails alone names itself.
|
|
548
|
+
const unsendable = (method, args, error) => {
|
|
549
|
+
const names = argumentNames[method] || [];
|
|
550
|
+
const index = args.findIndex((value, at) => names[at] && !sendable(value));
|
|
551
|
+
const cause = {
|
|
552
|
+
cause: error
|
|
553
|
+
};
|
|
554
|
+
return index < 0 ? Object.assign(fail('invalid-argument', `Invalid argument: it cannot be sent to the worker (${error.message})`), cause) : Object.assign(fail('invalid-argument', `Invalid ${names[index]}: it cannot be sent to the worker (${error.message})`, names[index]), cause);
|
|
555
|
+
};
|
|
556
|
+
|
|
526
557
|
// Like fetch: an aborted call rejects with the signal's reason, an AbortError unless the caller gave another.
|
|
527
558
|
const abortReason = signal => signal.reason !== undefined ? signal.reason : new DOMException('This operation was aborted', 'AbortError');
|
|
528
|
-
const imageFunctions = ['
|
|
529
|
-
const mappedImageFunctions = ['quantize', '
|
|
559
|
+
const imageFunctions = ['buildClustering', 'buildPalette', 'buildSwatches', 'quantize', 'quantizeWithPalette', 'quantizeToIndices', 'quantizeToIndicesWithPalette'];
|
|
560
|
+
const mappedImageFunctions = ['quantize', 'quantizeWithPalette'];
|
|
530
561
|
// The functions that only cluster, and so take no dither option.
|
|
531
|
-
const clusteringOnlyFunctions = ['
|
|
532
|
-
const otherFunctions = ['getPaletteFromClustering', 'getSwatchesFromClustering', 'getClusteringInfo'];
|
|
562
|
+
const clusteringOnlyFunctions = ['buildClustering', 'buildPalette', 'buildSwatches'];
|
|
533
563
|
|
|
534
564
|
// Calls each worker's `abandon` once the worker made by createWorker() has been garbage-collected (see `abandoned`).
|
|
535
565
|
const abandonment = typeof FinalizationRegistry === 'function' ? new FinalizationRegistry(abandon => abandon()) : null;
|
|
@@ -560,6 +590,9 @@ const makeWorker = (options, allowScript) => {
|
|
|
560
590
|
wasm: initialWasm
|
|
561
591
|
};
|
|
562
592
|
let initialising = null;
|
|
593
|
+
// Whether the worker script has said it is this version (the first message to every worker), and that exchange.
|
|
594
|
+
let shaken = false;
|
|
595
|
+
let shaking = null;
|
|
563
596
|
let connection = null;
|
|
564
597
|
let starting = null;
|
|
565
598
|
// Counts the workers this instance has started, so a late message or error from one it already dropped is ignored.
|
|
@@ -577,7 +610,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
577
610
|
let nextHandle = 1;
|
|
578
611
|
const pending = new Map();
|
|
579
612
|
// Whether a call made now is posted before it returns (see `own`).
|
|
580
|
-
const ready = () => connection !== null && !initial;
|
|
613
|
+
const ready = () => connection !== null && shaken && !initial;
|
|
581
614
|
// The image of a call made before the worker is ready: with `transfer: true` its buffer is moved out of the caller's
|
|
582
615
|
// hands at once (a structured clone that transfers it, so the caller's array empties now, as it would on posting),
|
|
583
616
|
// otherwise copied.
|
|
@@ -616,6 +649,8 @@ const makeWorker = (options, allowScript) => {
|
|
|
616
649
|
connection = null;
|
|
617
650
|
starting = null;
|
|
618
651
|
initialising = null;
|
|
652
|
+
shaken = false;
|
|
653
|
+
shaking = null;
|
|
619
654
|
if (initialWasm !== undefined) {
|
|
620
655
|
initial = {
|
|
621
656
|
wasm: initialWasm
|
|
@@ -716,6 +751,41 @@ const makeWorker = (options, allowScript) => {
|
|
|
716
751
|
}
|
|
717
752
|
connection = connection || started;
|
|
718
753
|
}
|
|
754
|
+
if (!shaken) {
|
|
755
|
+
// The first message to a worker: its script must be this version's (an older thread answers `handshake` as an
|
|
756
|
+
// unknown method; a 3.4-3.8 one would otherwise run calls but never see their signals or progress callbacks).
|
|
757
|
+
// Nothing else is posted before it answers, so a mismatch is reported to every call waiting on the worker.
|
|
758
|
+
if (!shaking) {
|
|
759
|
+
const mine = generation;
|
|
760
|
+
shaking = post('handshake', [], [], origin).then(theirs => {
|
|
761
|
+
if (theirs === version) {
|
|
762
|
+
shaken = true;
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
throw theirs;
|
|
766
|
+
}, error => {
|
|
767
|
+
// A coded error from the call itself (terminated, failed) is passed on; only a reply that is an error
|
|
768
|
+
// from the thread (an unknown method) means an older script.
|
|
769
|
+
if (error && error.code !== undefined && error.code !== 'invalid-argument') {
|
|
770
|
+
throw error;
|
|
771
|
+
}
|
|
772
|
+
throw undefined;
|
|
773
|
+
}).catch(theirs => {
|
|
774
|
+
if (theirs instanceof Error) {
|
|
775
|
+
throw theirs;
|
|
776
|
+
}
|
|
777
|
+
if (terminated) {
|
|
778
|
+
throw fail('worker-terminated', "Worker terminated.");
|
|
779
|
+
}
|
|
780
|
+
const reason = `The worker script is from another version of agglomerative-clustering (${typeof theirs === 'string' ? theirs : 'an older one'}; this is ${version}). Use the worker-thread.mjs from this version.`;
|
|
781
|
+
if (mine === generation) {
|
|
782
|
+
lose(reason);
|
|
783
|
+
}
|
|
784
|
+
throw fail('worker-failed', reason);
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
await shaking;
|
|
788
|
+
}
|
|
719
789
|
if (initial && !(method === 'init' && args.length > 0 && args[0].wasm !== undefined)) {
|
|
720
790
|
initialising = initialising || post('init', [initial], [], origin).then(() => {
|
|
721
791
|
initial = null;
|
|
@@ -818,10 +888,8 @@ const makeWorker = (options, allowScript) => {
|
|
|
818
888
|
connection.unref();
|
|
819
889
|
}
|
|
820
890
|
// A value that can't be structured-cloned (a function, symbol or promise) is an invalid argument, as it
|
|
821
|
-
// would be if the main entry were called with it.
|
|
822
|
-
reject(error && error.name === 'DataCloneError' ?
|
|
823
|
-
cause: error
|
|
824
|
-
}) : error);
|
|
891
|
+
// would be if the main entry were called with it; the first argument that fails alone is named.
|
|
892
|
+
reject(error && error.name === 'DataCloneError' ? unsendable(method, args.map(mark), error) : error);
|
|
825
893
|
}
|
|
826
894
|
});
|
|
827
895
|
};
|
|
@@ -831,6 +899,14 @@ const makeWorker = (options, allowScript) => {
|
|
|
831
899
|
instance[name] = (image, ...rest) => {
|
|
832
900
|
const origin = new Error();
|
|
833
901
|
return (async () => {
|
|
902
|
+
// A bare RGB or grey array is refused first, as in the main entry (before the options are read, an abort is
|
|
903
|
+
// honoured, or anything is posted or a transfer can empty the array).
|
|
904
|
+
const bare = bareImageError(image);
|
|
905
|
+
if (bare) {
|
|
906
|
+
throw bare;
|
|
907
|
+
}
|
|
908
|
+
// then the rest of the image's checks (its fields are read once here), before the options, as in the main entry
|
|
909
|
+
const checked = send(image);
|
|
834
910
|
const position = optionsPosition[name];
|
|
835
911
|
const {
|
|
836
912
|
options,
|
|
@@ -845,7 +921,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
845
921
|
// The main entry reports a wrong argument before an abort, so the worker checks the arguments (a
|
|
846
922
|
// copy, never transferred: an aborted call leaves the image with the caller) without doing the work.
|
|
847
923
|
try {
|
|
848
|
-
await invoke(name, [own(
|
|
924
|
+
await invoke(name, [own(checked), ...args.map(own)], [], origin, {
|
|
849
925
|
check: true
|
|
850
926
|
});
|
|
851
927
|
} catch (error) {
|
|
@@ -855,12 +931,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
855
931
|
}
|
|
856
932
|
throw abortReason(signal);
|
|
857
933
|
}
|
|
858
|
-
|
|
859
|
-
// array, and given once the call succeeds: the thread checks the other arguments, and its own copy is
|
|
860
|
-
// silenced (PRINCIPLES "Warnings").
|
|
861
|
-
const warning = bareImageWarning(image);
|
|
862
|
-
const byteLength = warning ? image.byteLength : 0;
|
|
863
|
-
let sent = send(image);
|
|
934
|
+
let sent = checked;
|
|
864
935
|
if (!ready()) {
|
|
865
936
|
sent = take(sent);
|
|
866
937
|
args = args.map(own);
|
|
@@ -869,21 +940,10 @@ const makeWorker = (options, allowScript) => {
|
|
|
869
940
|
signal,
|
|
870
941
|
onProgress
|
|
871
942
|
});
|
|
872
|
-
if (warning) {
|
|
873
|
-
warnOnce(warning, byteLength);
|
|
874
|
-
}
|
|
875
943
|
return mapped ? rebuild(image)(result) : result;
|
|
876
944
|
})();
|
|
877
945
|
};
|
|
878
946
|
}
|
|
879
|
-
for (const name of otherFunctions) {
|
|
880
|
-
instance[name] = (...args) => invoke(name, ready() ? args : args.map(own), [], new Error());
|
|
881
|
-
}
|
|
882
|
-
// 3.4.0's names for the functions that cluster an image. The old names are the same functions, and they still name the
|
|
883
|
-
// call on the wire, so a worker script copied from an older version still understands it.
|
|
884
|
-
instance.buildClustering = instance.getClustering;
|
|
885
|
-
instance.buildPalette = instance.getPalette;
|
|
886
|
-
instance.buildSwatches = instance.getSwatches;
|
|
887
947
|
instance.init = initArgs => {
|
|
888
948
|
const origin = new Error();
|
|
889
949
|
let checked;
|
|
@@ -1004,6 +1064,11 @@ const makeWorker = (options, allowScript) => {
|
|
|
1004
1064
|
if (released || !handle) {
|
|
1005
1065
|
throw fail('invalid-handle', "Invalid handle: it was released.");
|
|
1006
1066
|
}
|
|
1067
|
+
const bare = bareImageError(image);
|
|
1068
|
+
if (bare) {
|
|
1069
|
+
throw bare;
|
|
1070
|
+
}
|
|
1071
|
+
const checked = send(image);
|
|
1007
1072
|
const {
|
|
1008
1073
|
options,
|
|
1009
1074
|
signal,
|
|
@@ -1012,7 +1077,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
1012
1077
|
if (signal && signal.aborted) {
|
|
1013
1078
|
// As the functions on the worker do: a wrong argument is reported before the abort.
|
|
1014
1079
|
try {
|
|
1015
|
-
await invoke(method, [id, own(
|
|
1080
|
+
await invoke(method, [id, own(checked), k, options], [], origin, {
|
|
1016
1081
|
check: true
|
|
1017
1082
|
});
|
|
1018
1083
|
} catch (error) {
|
|
@@ -1022,9 +1087,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
1022
1087
|
}
|
|
1023
1088
|
throw abortReason(signal);
|
|
1024
1089
|
}
|
|
1025
|
-
|
|
1026
|
-
const byteLength = warning ? image.byteLength : 0;
|
|
1027
|
-
let sent = send(image);
|
|
1090
|
+
let sent = checked;
|
|
1028
1091
|
const ahead = !ready();
|
|
1029
1092
|
if (ahead) {
|
|
1030
1093
|
sent = take(sent);
|
|
@@ -1033,9 +1096,6 @@ const makeWorker = (options, allowScript) => {
|
|
|
1033
1096
|
signal,
|
|
1034
1097
|
onProgress
|
|
1035
1098
|
});
|
|
1036
|
-
if (warning) {
|
|
1037
|
-
warnOnce(warning, byteLength);
|
|
1038
|
-
}
|
|
1039
1099
|
return mapped ? rebuild(image)(result) : result;
|
|
1040
1100
|
})();
|
|
1041
1101
|
};
|
|
@@ -1058,6 +1118,12 @@ const makeWorker = (options, allowScript) => {
|
|
|
1058
1118
|
instance.cluster = (image, clusterOptions) => {
|
|
1059
1119
|
const origin = new Error();
|
|
1060
1120
|
return (async () => {
|
|
1121
|
+
// The image is checked before the options and the abort, as the main entry does.
|
|
1122
|
+
const bare = bareImageError(image);
|
|
1123
|
+
if (bare) {
|
|
1124
|
+
throw bare;
|
|
1125
|
+
}
|
|
1126
|
+
const checked = send(image);
|
|
1061
1127
|
const {
|
|
1062
1128
|
options,
|
|
1063
1129
|
signal,
|
|
@@ -1067,9 +1133,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
1067
1133
|
throw abortReason(signal);
|
|
1068
1134
|
}
|
|
1069
1135
|
const id = nextHandle++;
|
|
1070
|
-
const
|
|
1071
|
-
const byteLength = warning ? image.byteLength : 0;
|
|
1072
|
-
const sent = ready() ? send(image) : take(send(image));
|
|
1136
|
+
const sent = ready() ? checked : take(checked);
|
|
1073
1137
|
// Counted from before the call, so an abandoned worker isn't stopped between the call settling and the handle
|
|
1074
1138
|
// existing (see `abandoned`).
|
|
1075
1139
|
liveHandles++;
|
|
@@ -1088,9 +1152,6 @@ const makeWorker = (options, allowScript) => {
|
|
|
1088
1152
|
}
|
|
1089
1153
|
throw error;
|
|
1090
1154
|
}
|
|
1091
|
-
if (warning) {
|
|
1092
|
-
warnOnce(warning, byteLength);
|
|
1093
|
-
}
|
|
1094
1155
|
return makeHandle(id, 'image', rebuild(image));
|
|
1095
1156
|
})();
|
|
1096
1157
|
};
|
|
@@ -1173,17 +1234,9 @@ export const terminate = async () => {
|
|
|
1173
1234
|
};
|
|
1174
1235
|
export const init = (...args) => sharedWorker().init(...args);
|
|
1175
1236
|
export const buildClustering = (...args) => sharedWorker().buildClustering(...args);
|
|
1176
|
-
export const getClustering = buildClustering;
|
|
1177
1237
|
export const buildPalette = (...args) => sharedWorker().buildPalette(...args);
|
|
1178
|
-
export const getPalette = buildPalette;
|
|
1179
|
-
export const getPaletteFromClustering = (...args) => sharedWorker().getPaletteFromClustering(...args);
|
|
1180
1238
|
export const buildSwatches = (...args) => sharedWorker().buildSwatches(...args);
|
|
1181
|
-
export const getSwatches = buildSwatches;
|
|
1182
|
-
export const getSwatchesFromClustering = (...args) => sharedWorker().getSwatchesFromClustering(...args);
|
|
1183
|
-
export const getClusteringInfo = (...args) => sharedWorker().getClusteringInfo(...args);
|
|
1184
1239
|
export const quantize = (...args) => sharedWorker().quantize(...args);
|
|
1185
|
-
export const quantizeFromClustering = (...args) => sharedWorker().quantizeFromClustering(...args);
|
|
1186
1240
|
export const quantizeWithPalette = (...args) => sharedWorker().quantizeWithPalette(...args);
|
|
1187
1241
|
export const quantizeToIndices = (...args) => sharedWorker().quantizeToIndices(...args);
|
|
1188
|
-
export const quantizeToIndicesFromClustering = (...args) => sharedWorker().quantizeToIndicesFromClustering(...args);
|
|
1189
1242
|
export const quantizeToIndicesWithPalette = (...args) => sharedWorker().quantizeToIndicesWithPalette(...args);
|
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.3",
|
|
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",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
}
|
|
75
75
|
},
|
|
76
76
|
"engines": {
|
|
77
|
-
"node": "
|
|
77
|
+
"node": ">=22.12.0"
|
|
78
78
|
},
|
|
79
79
|
"files": [
|
|
80
80
|
"dist/",
|