agglomerative-clustering 3.7.2 → 4.0.0-beta.1
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 +9 -8
- package/dist/choose.cjs +166 -0
- package/dist/choose.mjs +160 -0
- package/dist/clustering.cjs +1 -1
- package/dist/clustering.js +1 -1
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +103 -64
- package/dist/index.d.cts +80 -19
- package/dist/index.d.ts +80 -19
- package/dist/index.mjs +102 -63
- package/dist/shared.cjs +129 -5
- package/dist/shared.mjs +123 -3
- package/dist/worker-thread.mjs +13 -2
- package/dist/worker.cjs +183 -64
- package/dist/worker.d.cts +51 -8
- package/dist/worker.d.ts +51 -8
- package/dist/worker.mjs +184 -65
- package/package.json +2 -2
package/dist/shared.cjs
CHANGED
|
@@ -6,14 +6,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.Clustering = Clustering;
|
|
7
7
|
exports.MARK = void 0;
|
|
8
8
|
exports.Palette = Palette;
|
|
9
|
-
exports.warnOnce = exports.version = exports.tooLarge = exports.sniffImageFormat = exports.snapshot = exports.sizeText = exports.silenceWarnings = exports.setEngine = exports.revive = exports.resetWarnings = exports.readHeader = exports.parseDither = exports.maxBytes = exports.markPalette = exports.markClustering = exports.mark = exports.looksLikeRgb = exports.latestVersion = exports.isUint8ClampedArray = exports.isUint8Array = exports.isPalette = exports.isOptionsObject = exports.isImageData = exports.isDetached = exports.isCodedError = exports.isClustering = exports.isArrayBufferLike = exports.imageHint = exports.imageFormats = exports.imageDataLike = exports.hasAlpha = exports.formatVersion = exports.formatNames = exports.fail = exports.errorCodes = exports.ditherSteps = exports.ditherMethods = exports.detachedError = exports.describe = exports.colorSpaces = exports.colorSpaceName = exports.colorSpaceCodes = exports.checkPalette = exports.checkImageDataPixels = exports.channels = exports.bareLayout = exports.bareImageWarning = exports.alphaModes = void 0;
|
|
9
|
+
exports.warnOnce = exports.version = exports.tooLarge = exports.sniffImageFormat = exports.snapshot = exports.sizeText = exports.silenceWarnings = exports.setEngine = exports.revive = exports.resetWarnings = exports.readHeader = exports.parseDither = exports.maxBytes = exports.markPalette = exports.markClustering = exports.mark = exports.looksLikeRgb = exports.latestVersion = exports.isUint8ClampedArray = exports.isUint8Array = exports.isPalette = exports.isOptionsObject = exports.isImageData = exports.isDetached = exports.isCodedError = exports.isClustering = exports.isArrayBufferLike = exports.imageHint = exports.imageFormats = exports.imageDataLike = exports.hasAlpha = exports.formatVersion = exports.formatNames = exports.formatHint = exports.fail = exports.errorCodes = exports.ditherSteps = exports.ditherMethods = exports.detachedError = exports.describe = exports.colorSpaces = exports.colorSpaceName = exports.colorSpaceCodes = exports.checkSize = exports.checkPalette = exports.checkImageObject = exports.checkImageKeys = exports.checkImageDataPixels = exports.channels = exports.bareLayout = exports.bareImageWarning = exports.alphaModes = void 0;
|
|
10
10
|
// Shared by both entries (index.js and worker.js) and the worker thread: errors, option snapshots, reading a clustering's
|
|
11
11
|
// header, the Palette and Clustering types, and the markers that carry those types across the worker boundary. Imported as
|
|
12
12
|
// './shared.js'; the build rewrites that to './shared.mjs' or './shared.cjs' (scripts/babel-import-extensions.cjs).
|
|
13
13
|
|
|
14
14
|
// The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
|
|
15
15
|
// is built from, src/version.hpp) in step with it. Do not edit it by hand.
|
|
16
|
-
const version = exports.version = '
|
|
16
|
+
const version = exports.version = '4.0.0-beta.1';
|
|
17
17
|
const colorSpaceCodes = exports.colorSpaceCodes = {
|
|
18
18
|
'rgb': 0,
|
|
19
19
|
'oklab': 1
|
|
@@ -184,10 +184,126 @@ const imageHint = image => {
|
|
|
184
184
|
return '';
|
|
185
185
|
};
|
|
186
186
|
|
|
187
|
+
// An image object has only these keys; any other own enumerable key (a decoder's result spread in) is an error, with a hint where
|
|
188
|
+
// the mistake is a common one. Inherited keys are not checked.
|
|
189
|
+
exports.imageHint = imageHint;
|
|
190
|
+
const imageKeys = ['data', 'format', 'width', 'height', 'premultiplied'];
|
|
191
|
+
const withinOne = (a, b) => {
|
|
192
|
+
if (a === b) {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
if (Math.abs(a.length - b.length) > 1) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
let i = 0;
|
|
199
|
+
while (i < a.length && i < b.length && a[i] === b[i]) {
|
|
200
|
+
i++;
|
|
201
|
+
}
|
|
202
|
+
if (a.length === b.length) {
|
|
203
|
+
// One substitution, or two neighbouring letters swapped (widht).
|
|
204
|
+
return a.slice(i + 1) === b.slice(i + 1) || a[i] === b[i + 1] && a[i + 1] === b[i] && a.slice(i + 2) === b.slice(i + 2);
|
|
205
|
+
}
|
|
206
|
+
const [long, short] = a.length > b.length ? [a, b] : [b, a];
|
|
207
|
+
return long.slice(i + 1) === short.slice(i);
|
|
208
|
+
};
|
|
209
|
+
const checkImageKeys = image => {
|
|
210
|
+
for (const key of Object.keys(image)) {
|
|
211
|
+
if (imageKeys.includes(key)) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
let hint = '';
|
|
215
|
+
if (key === 'premultiply') {
|
|
216
|
+
hint = ' Did you mean premultiplied: true?';
|
|
217
|
+
} else if (key === 'channels' || key === 'size' || key === 'depth') {
|
|
218
|
+
hint = " Take only width and height from a decoder's result.";
|
|
219
|
+
} else {
|
|
220
|
+
const near = imageKeys.find(known => key.toLowerCase() === known || withinOne(key, known));
|
|
221
|
+
if (near) {
|
|
222
|
+
hint = ` Did you mean '${near}'?`;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
throw fail('invalid-argument', `Invalid image: unknown key '${key}'. An image object has data, format, width, height and premultiplied: build it from those rather than spreading a decoder's result.${hint}`, 'image');
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// A hint for a format name that isn't one: the right case, the name other libraries use for the same bytes, or the nearest
|
|
230
|
+
// supported layout.
|
|
231
|
+
exports.checkImageKeys = checkImageKeys;
|
|
232
|
+
const formatHint = format => {
|
|
233
|
+
if (typeof format !== 'string') {
|
|
234
|
+
return '';
|
|
235
|
+
}
|
|
236
|
+
const lower = format.toLowerCase();
|
|
237
|
+
if (lower !== format && imageFormats.includes(lower)) {
|
|
238
|
+
return ` Format names are lower case: '${lower}'.`;
|
|
239
|
+
}
|
|
240
|
+
if (lower === 'xrgb' || lower === 'rgb24') {
|
|
241
|
+
return " Cairo RGB24 (xRGB) is BGRX in memory on little-endian machines: pass 'bgrx'.";
|
|
242
|
+
}
|
|
243
|
+
if (lower === 'argb' || lower === 'argb32') {
|
|
244
|
+
return " Cairo and node-canvas ARGB32 is BGRA in memory on little-endian machines, and premultiplied: pass it as 'bgra' with premultiplied: true (see Input in the API docs).";
|
|
245
|
+
}
|
|
246
|
+
if (lower === 'bgr' || lower === 'bgr24') {
|
|
247
|
+
return " 24-bit BGR isn't supported: widen each pixel to 4 bytes and pass 'bgrx'.";
|
|
248
|
+
}
|
|
249
|
+
return '';
|
|
250
|
+
};
|
|
251
|
+
// width and height are copied to results and used for dithering, so each must make sense on its own: a whole number of
|
|
252
|
+
// pixels that divides the pixel count (and, when both are given, account for exactly the pixels in the data).
|
|
253
|
+
exports.formatHint = formatHint;
|
|
254
|
+
const checkSize = (pixels, size, name, argument) => {
|
|
255
|
+
if (size === undefined) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (!Number.isInteger(size) || size < 0) {
|
|
259
|
+
throw fail('invalid-argument', `Invalid ${name}: must be a whole number of pixels.`, argument);
|
|
260
|
+
}
|
|
261
|
+
if (pixels > 0 && (size === 0 || pixels % size !== 0)) {
|
|
262
|
+
throw fail('invalid-argument', `Invalid ${name}: ${size} does not divide the image's ${pixels} pixels into whole rows.`, argument);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
// `premultiplied` on an image object: true only for 'rgba' and 'bgra' (the formats with alpha); false is accepted on any format.
|
|
267
|
+
exports.checkSize = checkSize;
|
|
268
|
+
const checkPremultiplied = image => {
|
|
269
|
+
const value = image.premultiplied;
|
|
270
|
+
if (value === undefined || value === false) {
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
if (value !== true) {
|
|
274
|
+
throw fail('invalid-argument', `Invalid image: premultiplied must be true or false, got ${describe(value)}.`, 'image');
|
|
275
|
+
}
|
|
276
|
+
if (image.format !== 'rgba' && image.format !== 'bgra') {
|
|
277
|
+
throw fail('invalid-argument', `Invalid image: premultiplied is for 'rgba' and 'bgra'; '${image.format}' has no alpha, so remove it.`, 'image');
|
|
278
|
+
}
|
|
279
|
+
return true;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
// Everything checked about an image object whose data is a Uint8Array or Uint8ClampedArray, in this order: format, data length,
|
|
283
|
+
// width and height, unknown keys, premultiplied. The main entry and the worker entry (before it posts) both run it, so their errors
|
|
284
|
+
// are identical. Returns the premultiplied flag.
|
|
285
|
+
const checkImageObject = image => {
|
|
286
|
+
if (!imageFormats.includes(image.format)) {
|
|
287
|
+
throw fail('invalid-argument', `Invalid format: got ${describe(image.format)}; it must be ${formatNames}.${formatHint(image.format)}`, 'image');
|
|
288
|
+
}
|
|
289
|
+
const stride = channels(image.format);
|
|
290
|
+
if (image.data.length % stride !== 0) {
|
|
291
|
+
throw fail('invalid-argument', `Invalid image data: length must be a multiple of ${stride} for '${image.format}'.`, 'image');
|
|
292
|
+
}
|
|
293
|
+
const pixels = image.data.length / stride;
|
|
294
|
+
checkSize(pixels, image.width, 'image width', 'image');
|
|
295
|
+
checkSize(pixels, image.height, 'image height', 'image');
|
|
296
|
+
if (image.width !== undefined && image.height !== undefined && image.width * image.height !== pixels) {
|
|
297
|
+
throw fail('invalid-argument', `Invalid image: width x height (${image.width} x ${image.height}) does not match its ${pixels} pixels.`, 'image');
|
|
298
|
+
}
|
|
299
|
+
checkImageKeys(image);
|
|
300
|
+
return checkPremultiplied(image);
|
|
301
|
+
};
|
|
302
|
+
|
|
187
303
|
// An options object is an ordinary object. A Map, Date, ArrayBuffer or typed array has no own keys and would pass as
|
|
188
304
|
// `{}`, silently ignoring what the caller meant. Checked by tag rather than prototype so objects from another realm
|
|
189
305
|
// (an iframe, a test sandbox) are fine.
|
|
190
|
-
exports.
|
|
306
|
+
exports.checkImageObject = checkImageObject;
|
|
191
307
|
const isOptionsObject = value => value !== null && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]';
|
|
192
308
|
|
|
193
309
|
// One plain copy of an options object, taken once: every enumerable property, own or inherited (so options layered with
|
|
@@ -597,7 +713,8 @@ const methodNames = {
|
|
|
597
713
|
getSwatches: ['getSwatches(k)', 'getSwatchesFromClustering(clustering, k)'],
|
|
598
714
|
getClusteringInfo: ['getClusteringInfo()', 'getClusteringInfo(clustering)'],
|
|
599
715
|
quantize: ['quantize(image, k)', 'quantizeFromClustering(image, clustering, k)'],
|
|
600
|
-
quantizeToIndices: ['quantizeToIndices(image, k)', 'quantizeToIndicesFromClustering(image, clustering, k)']
|
|
716
|
+
quantizeToIndices: ['quantizeToIndices(image, k)', 'quantizeToIndicesFromClustering(image, clustering, k)'],
|
|
717
|
+
chooseFrom: ['chooseFrom(palette, k, options?)', "a loaded handle's chooseFrom: (await worker.load(clustering)).chooseFrom(palette, k)"]
|
|
601
718
|
};
|
|
602
719
|
// Checks the receiver before anything else, since it is written first, then (for the mapping methods) that the image isn't
|
|
603
720
|
// the worker handle's `k`, then that the main entry is loaded.
|
|
@@ -611,7 +728,7 @@ const receive = (name, receiver, image) => {
|
|
|
611
728
|
checkImageFirst(name, image.value);
|
|
612
729
|
}
|
|
613
730
|
if (engine === null) {
|
|
614
|
-
throw fail('wasm-load-failed', `clustering.${name} can't run here: a clustering's methods run on the calling thread with the main entry, 'agglomerative-clustering', of the copy of the package that made it, and that entry isn't loaded on this thread (this clustering came from the worker entry, or from another copy, such as the other module system's build). Import 'agglomerative-clustering' in this thread (and call its init({ wasm }) if you set where the module comes from), wrap the bytes with new Clustering(bytes) from the main entry you use, or call ${form} from 'agglomerative-clustering/worker', which runs in the worker.`);
|
|
731
|
+
throw fail('wasm-load-failed', `clustering.${name} can't run here: a clustering's methods run on the calling thread with the main entry, 'agglomerative-clustering', of the copy of the package that made it, and that entry isn't loaded on this thread (this clustering came from the worker entry, or from another copy, such as the other module system's build). Import 'agglomerative-clustering' in this thread (and call its init({ wasm }) if you set where the module comes from), wrap the bytes with new Clustering(bytes) from the main entry you use, or ${form.startsWith('a ') ? `use ${form}` : `call ${form} from 'agglomerative-clustering/worker'`}, which runs in the worker.`);
|
|
615
732
|
}
|
|
616
733
|
return engine;
|
|
617
734
|
};
|
|
@@ -661,6 +778,13 @@ const methods = {
|
|
|
661
778
|
} catch (error) {
|
|
662
779
|
return Promise.reject(error);
|
|
663
780
|
}
|
|
781
|
+
},
|
|
782
|
+
chooseFrom(palette, k, options) {
|
|
783
|
+
try {
|
|
784
|
+
return receive('chooseFrom', this).chooseFrom('clustering.chooseFrom', this, palette, k, options);
|
|
785
|
+
} catch (error) {
|
|
786
|
+
return Promise.reject(error);
|
|
787
|
+
}
|
|
664
788
|
}
|
|
665
789
|
};
|
|
666
790
|
// Writable and configurable like any method (so `vi.spyOn(clustering, 'getPalette')` works), and not enumerable.
|
package/dist/shared.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
// The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
|
|
6
6
|
// is built from, src/version.hpp) in step with it. Do not edit it by hand.
|
|
7
|
-
export const version = '
|
|
7
|
+
export const version = '4.0.0-beta.1';
|
|
8
8
|
export const colorSpaceCodes = {
|
|
9
9
|
'rgb': 0,
|
|
10
10
|
'oklab': 1
|
|
@@ -160,6 +160,118 @@ export const imageHint = image => {
|
|
|
160
160
|
return '';
|
|
161
161
|
};
|
|
162
162
|
|
|
163
|
+
// An image object has only these keys; any other own enumerable key (a decoder's result spread in) is an error, with a hint where
|
|
164
|
+
// the mistake is a common one. Inherited keys are not checked.
|
|
165
|
+
const imageKeys = ['data', 'format', 'width', 'height', 'premultiplied'];
|
|
166
|
+
const withinOne = (a, b) => {
|
|
167
|
+
if (a === b) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
if (Math.abs(a.length - b.length) > 1) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
let i = 0;
|
|
174
|
+
while (i < a.length && i < b.length && a[i] === b[i]) {
|
|
175
|
+
i++;
|
|
176
|
+
}
|
|
177
|
+
if (a.length === b.length) {
|
|
178
|
+
// One substitution, or two neighbouring letters swapped (widht).
|
|
179
|
+
return a.slice(i + 1) === b.slice(i + 1) || a[i] === b[i + 1] && a[i + 1] === b[i] && a.slice(i + 2) === b.slice(i + 2);
|
|
180
|
+
}
|
|
181
|
+
const [long, short] = a.length > b.length ? [a, b] : [b, a];
|
|
182
|
+
return long.slice(i + 1) === short.slice(i);
|
|
183
|
+
};
|
|
184
|
+
export const checkImageKeys = image => {
|
|
185
|
+
for (const key of Object.keys(image)) {
|
|
186
|
+
if (imageKeys.includes(key)) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
let hint = '';
|
|
190
|
+
if (key === 'premultiply') {
|
|
191
|
+
hint = ' Did you mean premultiplied: true?';
|
|
192
|
+
} else if (key === 'channels' || key === 'size' || key === 'depth') {
|
|
193
|
+
hint = " Take only width and height from a decoder's result.";
|
|
194
|
+
} else {
|
|
195
|
+
const near = imageKeys.find(known => key.toLowerCase() === known || withinOne(key, known));
|
|
196
|
+
if (near) {
|
|
197
|
+
hint = ` Did you mean '${near}'?`;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
throw fail('invalid-argument', `Invalid image: unknown key '${key}'. An image object has data, format, width, height and premultiplied: build it from those rather than spreading a decoder's result.${hint}`, 'image');
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
// A hint for a format name that isn't one: the right case, the name other libraries use for the same bytes, or the nearest
|
|
205
|
+
// supported layout.
|
|
206
|
+
export const formatHint = format => {
|
|
207
|
+
if (typeof format !== 'string') {
|
|
208
|
+
return '';
|
|
209
|
+
}
|
|
210
|
+
const lower = format.toLowerCase();
|
|
211
|
+
if (lower !== format && imageFormats.includes(lower)) {
|
|
212
|
+
return ` Format names are lower case: '${lower}'.`;
|
|
213
|
+
}
|
|
214
|
+
if (lower === 'xrgb' || lower === 'rgb24') {
|
|
215
|
+
return " Cairo RGB24 (xRGB) is BGRX in memory on little-endian machines: pass 'bgrx'.";
|
|
216
|
+
}
|
|
217
|
+
if (lower === 'argb' || lower === 'argb32') {
|
|
218
|
+
return " Cairo and node-canvas ARGB32 is BGRA in memory on little-endian machines, and premultiplied: pass it as 'bgra' with premultiplied: true (see Input in the API docs).";
|
|
219
|
+
}
|
|
220
|
+
if (lower === 'bgr' || lower === 'bgr24') {
|
|
221
|
+
return " 24-bit BGR isn't supported: widen each pixel to 4 bytes and pass 'bgrx'.";
|
|
222
|
+
}
|
|
223
|
+
return '';
|
|
224
|
+
};
|
|
225
|
+
// width and height are copied to results and used for dithering, so each must make sense on its own: a whole number of
|
|
226
|
+
// pixels that divides the pixel count (and, when both are given, account for exactly the pixels in the data).
|
|
227
|
+
export const checkSize = (pixels, size, name, argument) => {
|
|
228
|
+
if (size === undefined) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (!Number.isInteger(size) || size < 0) {
|
|
232
|
+
throw fail('invalid-argument', `Invalid ${name}: must be a whole number of pixels.`, argument);
|
|
233
|
+
}
|
|
234
|
+
if (pixels > 0 && (size === 0 || pixels % size !== 0)) {
|
|
235
|
+
throw fail('invalid-argument', `Invalid ${name}: ${size} does not divide the image's ${pixels} pixels into whole rows.`, argument);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
// `premultiplied` on an image object: true only for 'rgba' and 'bgra' (the formats with alpha); false is accepted on any format.
|
|
240
|
+
const checkPremultiplied = image => {
|
|
241
|
+
const value = image.premultiplied;
|
|
242
|
+
if (value === undefined || value === false) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
if (value !== true) {
|
|
246
|
+
throw fail('invalid-argument', `Invalid image: premultiplied must be true or false, got ${describe(value)}.`, 'image');
|
|
247
|
+
}
|
|
248
|
+
if (image.format !== 'rgba' && image.format !== 'bgra') {
|
|
249
|
+
throw fail('invalid-argument', `Invalid image: premultiplied is for 'rgba' and 'bgra'; '${image.format}' has no alpha, so remove it.`, 'image');
|
|
250
|
+
}
|
|
251
|
+
return true;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// Everything checked about an image object whose data is a Uint8Array or Uint8ClampedArray, in this order: format, data length,
|
|
255
|
+
// width and height, unknown keys, premultiplied. The main entry and the worker entry (before it posts) both run it, so their errors
|
|
256
|
+
// are identical. Returns the premultiplied flag.
|
|
257
|
+
export const checkImageObject = image => {
|
|
258
|
+
if (!imageFormats.includes(image.format)) {
|
|
259
|
+
throw fail('invalid-argument', `Invalid format: got ${describe(image.format)}; it must be ${formatNames}.${formatHint(image.format)}`, 'image');
|
|
260
|
+
}
|
|
261
|
+
const stride = channels(image.format);
|
|
262
|
+
if (image.data.length % stride !== 0) {
|
|
263
|
+
throw fail('invalid-argument', `Invalid image data: length must be a multiple of ${stride} for '${image.format}'.`, 'image');
|
|
264
|
+
}
|
|
265
|
+
const pixels = image.data.length / stride;
|
|
266
|
+
checkSize(pixels, image.width, 'image width', 'image');
|
|
267
|
+
checkSize(pixels, image.height, 'image height', 'image');
|
|
268
|
+
if (image.width !== undefined && image.height !== undefined && image.width * image.height !== pixels) {
|
|
269
|
+
throw fail('invalid-argument', `Invalid image: width x height (${image.width} x ${image.height}) does not match its ${pixels} pixels.`, 'image');
|
|
270
|
+
}
|
|
271
|
+
checkImageKeys(image);
|
|
272
|
+
return checkPremultiplied(image);
|
|
273
|
+
};
|
|
274
|
+
|
|
163
275
|
// An options object is an ordinary object. A Map, Date, ArrayBuffer or typed array has no own keys and would pass as
|
|
164
276
|
// `{}`, silently ignoring what the caller meant. Checked by tag rather than prototype so objects from another realm
|
|
165
277
|
// (an iframe, a test sandbox) are fine.
|
|
@@ -557,7 +669,8 @@ const methodNames = {
|
|
|
557
669
|
getSwatches: ['getSwatches(k)', 'getSwatchesFromClustering(clustering, k)'],
|
|
558
670
|
getClusteringInfo: ['getClusteringInfo()', 'getClusteringInfo(clustering)'],
|
|
559
671
|
quantize: ['quantize(image, k)', 'quantizeFromClustering(image, clustering, k)'],
|
|
560
|
-
quantizeToIndices: ['quantizeToIndices(image, k)', 'quantizeToIndicesFromClustering(image, clustering, k)']
|
|
672
|
+
quantizeToIndices: ['quantizeToIndices(image, k)', 'quantizeToIndicesFromClustering(image, clustering, k)'],
|
|
673
|
+
chooseFrom: ['chooseFrom(palette, k, options?)', "a loaded handle's chooseFrom: (await worker.load(clustering)).chooseFrom(palette, k)"]
|
|
561
674
|
};
|
|
562
675
|
// Checks the receiver before anything else, since it is written first, then (for the mapping methods) that the image isn't
|
|
563
676
|
// the worker handle's `k`, then that the main entry is loaded.
|
|
@@ -571,7 +684,7 @@ const receive = (name, receiver, image) => {
|
|
|
571
684
|
checkImageFirst(name, image.value);
|
|
572
685
|
}
|
|
573
686
|
if (engine === null) {
|
|
574
|
-
throw fail('wasm-load-failed', `clustering.${name} can't run here: a clustering's methods run on the calling thread with the main entry, 'agglomerative-clustering', of the copy of the package that made it, and that entry isn't loaded on this thread (this clustering came from the worker entry, or from another copy, such as the other module system's build). Import 'agglomerative-clustering' in this thread (and call its init({ wasm }) if you set where the module comes from), wrap the bytes with new Clustering(bytes) from the main entry you use, or call ${form} from 'agglomerative-clustering/worker', which runs in the worker.`);
|
|
687
|
+
throw fail('wasm-load-failed', `clustering.${name} can't run here: a clustering's methods run on the calling thread with the main entry, 'agglomerative-clustering', of the copy of the package that made it, and that entry isn't loaded on this thread (this clustering came from the worker entry, or from another copy, such as the other module system's build). Import 'agglomerative-clustering' in this thread (and call its init({ wasm }) if you set where the module comes from), wrap the bytes with new Clustering(bytes) from the main entry you use, or ${form.startsWith('a ') ? `use ${form}` : `call ${form} from 'agglomerative-clustering/worker'`}, which runs in the worker.`);
|
|
575
688
|
}
|
|
576
689
|
return engine;
|
|
577
690
|
};
|
|
@@ -621,6 +734,13 @@ const methods = {
|
|
|
621
734
|
} catch (error) {
|
|
622
735
|
return Promise.reject(error);
|
|
623
736
|
}
|
|
737
|
+
},
|
|
738
|
+
chooseFrom(palette, k, options) {
|
|
739
|
+
try {
|
|
740
|
+
return receive('chooseFrom', this).chooseFrom('clustering.chooseFrom', this, palette, k, options);
|
|
741
|
+
} catch (error) {
|
|
742
|
+
return Promise.reject(error);
|
|
743
|
+
}
|
|
624
744
|
}
|
|
625
745
|
};
|
|
626
746
|
// Writable and configurable like any method (so `vi.spyOn(clustering, 'getPalette')` works), and not enumerable.
|
package/dist/worker-thread.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { MARK, mark, revive, isPalette, silenceWarnings } from "./shared.mjs";
|
|
|
6
6
|
// The caller's side checks for warnings and gives them (PRINCIPLES "Warnings"), so this copy stays quiet.
|
|
7
7
|
silenceWarnings();
|
|
8
8
|
|
|
9
|
-
// Images kept in the worker by `cluster`, with their clusterings, by handle id.
|
|
9
|
+
// Images kept in the worker by `cluster`, with their clusterings, and clusterings alone from `load`, by handle id.
|
|
10
10
|
const handles = new Map();
|
|
11
11
|
const handle = id => {
|
|
12
12
|
const entry = handles.get(id);
|
|
@@ -50,6 +50,11 @@ const methods = {
|
|
|
50
50
|
clustering: await ac.getClustering(image, options)
|
|
51
51
|
});
|
|
52
52
|
},
|
|
53
|
+
load: async (id, clustering) => {
|
|
54
|
+
handles.set(id, {
|
|
55
|
+
clustering: new ac.Clustering(clustering)
|
|
56
|
+
});
|
|
57
|
+
},
|
|
53
58
|
release: async id => {
|
|
54
59
|
handles.delete(id);
|
|
55
60
|
},
|
|
@@ -57,8 +62,11 @@ const methods = {
|
|
|
57
62
|
handleGetPalette: async (id, k) => ac.getPaletteFromClustering(handle(id).clustering, k),
|
|
58
63
|
handleGetSwatches: async (id, k) => ac.getSwatchesFromClustering(handle(id).clustering, k),
|
|
59
64
|
handleGetClusteringInfo: async id => ac.getClusteringInfo(handle(id).clustering),
|
|
65
|
+
handleChooseFrom: async (id, palette, k, options) => handle(id).clustering.chooseFrom(palette, k, options),
|
|
60
66
|
handleQuantize: async (id, k, options) => ac.quantizeFromClustering(handle(id).image, handle(id).clustering, k, options),
|
|
61
67
|
handleQuantizeToIndices: async (id, k, options) => ac.quantizeToIndicesFromClustering(handle(id).image, handle(id).clustering, k, options),
|
|
68
|
+
handleQuantizeImage: async (id, image, k, options) => ac.quantizeFromClustering(image, handle(id).clustering, k, options),
|
|
69
|
+
handleQuantizeToIndicesImage: async (id, image, k, options) => ac.quantizeToIndicesFromClustering(image, handle(id).clustering, k, options),
|
|
62
70
|
handleQuantizeWithPalette: async (id, palette, options) => ac.quantizeWithPalette(handle(id).image, palette, await inClusteringSpace(id, options, palette)),
|
|
63
71
|
handleQuantizeToIndicesWithPalette: async (id, palette, options) => ac.quantizeToIndicesWithPalette(handle(id).image, palette, await inClusteringSpace(id, options, palette))
|
|
64
72
|
};
|
|
@@ -92,7 +100,10 @@ const optionsIndex = {
|
|
|
92
100
|
handleQuantize: 2,
|
|
93
101
|
handleQuantizeToIndices: 2,
|
|
94
102
|
handleQuantizeWithPalette: 2,
|
|
95
|
-
handleQuantizeToIndicesWithPalette: 2
|
|
103
|
+
handleQuantizeToIndicesWithPalette: 2,
|
|
104
|
+
handleChooseFrom: 3,
|
|
105
|
+
handleQuantizeImage: 3,
|
|
106
|
+
handleQuantizeToIndicesImage: 3
|
|
96
107
|
};
|
|
97
108
|
|
|
98
109
|
// One AbortController per call that can be cancelled, by call id. It exists from the moment the call arrives, so a call
|