agglomerative-clustering 3.5.0 → 3.7.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 +1 -1
- package/dist/clustering.cjs +1 -1
- package/dist/clustering.js +1 -1
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +130 -16
- package/dist/index.d.cts +13 -15
- package/dist/index.d.ts +13 -15
- package/dist/index.mjs +131 -17
- package/dist/shared.cjs +88 -49
- package/dist/shared.mjs +84 -46
- package/dist/worker-thread.mjs +2 -2
- package/dist/worker.cjs +9 -11
- package/dist/worker.mjs +10 -12
- package/package.json +2 -2
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 = '3.
|
|
7
|
+
export const version = '3.7.0';
|
|
8
8
|
export const colorSpaceCodes = {
|
|
9
9
|
'rgb': 0,
|
|
10
10
|
'oklab': 1
|
|
@@ -340,30 +340,44 @@ export const sniffImageFormat = bytes => {
|
|
|
340
340
|
return null;
|
|
341
341
|
};
|
|
342
342
|
|
|
343
|
-
// ----- RGB bytes passed bare
|
|
344
|
-
// The other common way to get a bare array wrong is RGB
|
|
345
|
-
//
|
|
346
|
-
//
|
|
343
|
+
// ----- RGB and grey bytes passed bare -------------------------------------------------------------------------------
|
|
344
|
+
// The other common way to get a bare array wrong is bytes of another layout: RGB (sharp's plain .raw()), or grey and grey
|
|
345
|
+
// with alpha (fast-png's decode() of a grey PNG). Read as RGBA, every pixel takes bytes from its neighbours and one of them
|
|
346
|
+
// as its alpha, and nothing fails while the length divides by 4. They are never reinterpreted (a bare array is RGBA,
|
|
347
|
+
// PRINCIPLES), only reported: a warning in 3.x, an error from 4.0.
|
|
347
348
|
//
|
|
348
|
-
// The
|
|
349
|
-
//
|
|
350
|
-
//
|
|
351
|
-
//
|
|
352
|
-
//
|
|
353
|
-
//
|
|
354
|
-
// (
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
349
|
+
// The rules use E(k), the mean change between a byte and the one k before it (bench/spikes/bare-rgb/ and
|
|
350
|
+
// bench/spikes/bare-grey/: no false positives on 2,984 real RGBA images).
|
|
351
|
+
// - RGB, decided first: real RGBA changes least at a step of 4, RGB at a step of 3. A premultiplied white mask,
|
|
352
|
+
// (a, a, a, a), also scores high, so pixels with four equal bytes overrule it. Grey RGB is too smooth for that score,
|
|
353
|
+
// but where it changes its bytes come in equal triples at a step of 3 and not of 4.
|
|
354
|
+
// - Grey and alpha (2 bytes a pixel): smooth at a step of 2 and rough at a step of 1, since grey and alpha bytes alternate.
|
|
355
|
+
// - Grey (1 byte a pixel): smoothest at a step of 1, unless most changing pixels have four equal bytes, which is real
|
|
356
|
+
// RGBA (grey with alpha = grey). The RGB rule goes first because near-grey RGB can look the same; it then keeps the
|
|
357
|
+
// rgb-as-rgba code, whose text covers grey too.
|
|
358
|
+
// Units equal to the one before are skipped by the equal-byte counts, since flat areas read the same either way. It
|
|
359
|
+
// samples 512 windows of 96 bytes (each on a multiple of 12, a pixel under every reading, one in each 512th of the
|
|
360
|
+
// array), so it costs about 0.35 ms at any size. Returns 'rgb', 'grey', 'grey-alpha' or null.
|
|
361
|
+
const bareFloor = 192,
|
|
362
|
+
bareWindow = 96,
|
|
363
|
+
bareWindows = 512;
|
|
364
|
+
export const bareLayout = d => {
|
|
365
|
+
if (d.length < bareFloor) {
|
|
366
|
+
return null;
|
|
361
367
|
}
|
|
362
|
-
const whole = d.length <=
|
|
363
|
-
const windows = whole ? 1 :
|
|
364
|
-
size = whole ? d.length :
|
|
365
|
-
const step = whole ? 0 : Math.floor((d.length -
|
|
366
|
-
|
|
368
|
+
const whole = d.length <= bareWindow * bareWindows;
|
|
369
|
+
const windows = whole ? 1 : bareWindows,
|
|
370
|
+
size = whole ? d.length : bareWindow;
|
|
371
|
+
const step = whole ? 0 : Math.floor((d.length - bareWindow) / (bareWindows - 1) / 12) * 12;
|
|
372
|
+
// Each window starts at a fixed pseudo-random multiple of 12 within its slot. Evenly spaced starts would alias with
|
|
373
|
+
// the image's rows (768×512 RGBA spaces them exactly one row apart), so every window would see only the left edge,
|
|
374
|
+
// and a cutout's transparent margin would hide the rest of the image.
|
|
375
|
+
const slots = step / 12,
|
|
376
|
+
last = d.length - size;
|
|
377
|
+
const startOf = w => whole ? 0 : Math.min(w * step + (Math.imul(w + 1, 0x9E3779B1) >>> 0) % slots * 12, last - last % 12);
|
|
378
|
+
let e1 = 0,
|
|
379
|
+
e2 = 0,
|
|
380
|
+
e3 = 0,
|
|
367
381
|
e4 = 0,
|
|
368
382
|
n = 0,
|
|
369
383
|
g3 = 0,
|
|
@@ -372,14 +386,18 @@ export const looksLikeRgb = d => {
|
|
|
372
386
|
q4 = 0,
|
|
373
387
|
n4 = 0;
|
|
374
388
|
for (let w = 0; w < windows; w++) {
|
|
375
|
-
const start = w
|
|
389
|
+
const start = startOf(w),
|
|
376
390
|
end = start + size;
|
|
377
391
|
for (let i = start + 4; i < end; i++) {
|
|
378
392
|
const v = d[i],
|
|
379
|
-
a = v - d[i -
|
|
380
|
-
b = v - d[i -
|
|
381
|
-
|
|
382
|
-
|
|
393
|
+
a = v - d[i - 1],
|
|
394
|
+
b = v - d[i - 2],
|
|
395
|
+
c = v - d[i - 3],
|
|
396
|
+
e = v - d[i - 4];
|
|
397
|
+
e1 += a < 0 ? -a : a;
|
|
398
|
+
e2 += b < 0 ? -b : b;
|
|
399
|
+
e3 += c < 0 ? -c : c;
|
|
400
|
+
e4 += e < 0 ? -e : e;
|
|
383
401
|
}
|
|
384
402
|
n += size - 4;
|
|
385
403
|
for (let i = start + 3; i + 2 < end; i += 3) {
|
|
@@ -404,17 +422,21 @@ export const looksLikeRgb = d => {
|
|
|
404
422
|
}
|
|
405
423
|
}
|
|
406
424
|
}
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
425
|
+
const E1 = e1 / n + 0.05,
|
|
426
|
+
E2 = e2 / n + 0.05,
|
|
427
|
+
E3 = e3 / n + 0.05,
|
|
428
|
+
E4 = e4 / n + 0.05;
|
|
429
|
+
const fewQuads = q4 < 0.5 * Math.max(1, n4);
|
|
430
|
+
const layout = E4 / E3 > 1.25 && fewQuads || g3 > 0.9 * Math.max(1, n3) && g4 < 0.6 * Math.max(1, n4) ? 'rgb' : E4 / E2 > 1.1 && E2 / E1 < 0.5 ? 'grey-alpha' : E4 / E1 > 1.5 && fewQuads ? 'grey' : null;
|
|
431
|
+
if (layout === null) {
|
|
432
|
+
return null;
|
|
411
433
|
}
|
|
412
|
-
// Bytes that mostly rise by one fixed step ((i * k) & 255, the usual fake image in tests) are pixels of
|
|
413
|
-
//
|
|
414
|
-
// so RGBA (the usual input) doesn't pay for it.
|
|
434
|
+
// Bytes that mostly rise by one fixed step ((i * k) & 255, the usual fake image in tests) are pixels of no layout, and
|
|
435
|
+
// score 4/3 as RGB or 4 as grey whatever the step. Counted only for arrays about to be flagged, a second pass over the
|
|
436
|
+
// same windows, so RGBA (the usual input) doesn't pay for it.
|
|
415
437
|
let ramp = 0;
|
|
416
438
|
for (let w = 0; w < windows; w++) {
|
|
417
|
-
const start = w
|
|
439
|
+
const start = startOf(w),
|
|
418
440
|
end = start + size;
|
|
419
441
|
for (let i = start + 4; i < end; i++) {
|
|
420
442
|
const rise = d[i] - d[i - 1] & 255;
|
|
@@ -423,23 +445,34 @@ export const looksLikeRgb = d => {
|
|
|
423
445
|
}
|
|
424
446
|
}
|
|
425
447
|
}
|
|
426
|
-
return ramp <= 0.5 * n;
|
|
448
|
+
return ramp <= 0.5 * n ? layout : null;
|
|
427
449
|
};
|
|
450
|
+
export const looksLikeRgb = d => bareLayout(d) === 'rgb';
|
|
428
451
|
|
|
429
452
|
// Codes already warned in this copy of the library (PRINCIPLES: once per code, per copy).
|
|
430
453
|
const warned = new Set();
|
|
454
|
+
const bareCodes = {
|
|
455
|
+
rgb: 'rgb-as-rgba',
|
|
456
|
+
grey: 'gray-as-rgba',
|
|
457
|
+
'grey-alpha': 'gray-as-rgba'
|
|
458
|
+
};
|
|
431
459
|
|
|
432
|
-
// The warning a bare image calls for, or null. It skips everything the main entry rejects (palettes
|
|
433
|
-
// passed as the image, encoded files, lengths that aren't a multiple of 4), so a call never both warns
|
|
434
|
-
// rejects it, and both entries share this one order.
|
|
435
|
-
|
|
436
|
-
|
|
460
|
+
// The warning a bare image calls for, as {code, layout}, or null. It skips everything the main entry rejects (palettes
|
|
461
|
+
// and clusterings passed as the image, encoded files, lengths that aren't a multiple of 4), so a call never both warns
|
|
462
|
+
// about its image and rejects it, and both entries share this one order. An array of one layout never takes another's
|
|
463
|
+
// code because its own was already used: it is classified first, and only then is its code checked.
|
|
464
|
+
export const bareImageWarning = image => {
|
|
465
|
+
if (warned.has('rgb-as-rgba') && warned.has('gray-as-rgba') || !isUint8Array(image) || isPalette(image) || isClustering(image)) {
|
|
437
466
|
return null;
|
|
438
467
|
}
|
|
439
468
|
if (sniffImageFormat(image) !== null || image.length % 4 !== 0) {
|
|
440
469
|
return null;
|
|
441
470
|
}
|
|
442
|
-
|
|
471
|
+
const layout = bareLayout(image);
|
|
472
|
+
return layout === null || warned.has(bareCodes[layout]) ? null : {
|
|
473
|
+
code: bareCodes[layout],
|
|
474
|
+
layout
|
|
475
|
+
};
|
|
443
476
|
};
|
|
444
477
|
|
|
445
478
|
// The text of each warning. Delivered through process.emitWarning in Node, Bun and Deno (--no-warnings,
|
|
@@ -449,14 +482,19 @@ export const bareRgbWarning = image => {
|
|
|
449
482
|
// keeps Parcel from adding one.
|
|
450
483
|
const nodeLike = () => typeof navigator === 'undefined' || /^(Node\.js|Bun|Deno)\//.test(String(navigator.userAgent));
|
|
451
484
|
const warningText = {
|
|
452
|
-
'rgb-as-rgba': byteLength => `This image (${byteLength.toLocaleString('en-US')} bytes) looks like RGB bytes (3 per pixel) passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. If it is RGB, pass { data, format: 'rgb' } (sharp's .raw() gives RGB for an image without alpha unless you add .ensureAlpha()). If it really is RGBA, pass { data, format: 'rgba' } to say so. From 4.0 this is an error. Shown once
|
|
485
|
+
'rgb-as-rgba': byteLength => `This image (${byteLength.toLocaleString('en-US')} bytes) looks like RGB bytes (3 per pixel), or grey, passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. If it is RGB, pass { data, format: 'rgb' } (sharp's .raw() gives RGB for an image without alpha unless you add .ensureAlpha()). If it is grey (fast-png's decode() of a grey PNG), expand it to RGBA; format: 'rgb' would misread it too. If it really is RGBA, pass { data, format: 'rgba' } to say so. From 4.0 this is an error. Shown once.`,
|
|
486
|
+
'gray-as-rgba': (byteLength, layout) => (layout === 'grey-alpha' ? `This image (${byteLength.toLocaleString('en-US')} bytes) looks like grey and alpha bytes (2 per pixel) passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. fast-png's decode() gives this for a grey PNG with alpha.` : `This image (${byteLength.toLocaleString('en-US')} bytes) looks like grey bytes (1 per pixel) passed as a bare Uint8Array, which is read as RGBA (4 per pixel) and would give wrong colours. fast-png's decode() gives this for a grey PNG (rows below 8 bits stay packed) and for a palette PNG's indices.`) + ` Expand ${layout === 'grey-alpha' ? 'it' : '8-bit grey'} to RGBA, or decode with something that gives RGBA (pngjs, or sharp with .ensureAlpha()). There is no grey format to pass. If it really is RGBA, pass { data, format: 'rgba' } to say so. From 4.0 this is an error. Shown once.`
|
|
453
487
|
};
|
|
454
|
-
|
|
488
|
+
// `warning` is what bareImageWarning returned: {code, layout}.
|
|
489
|
+
export const warnOnce = ({
|
|
490
|
+
code,
|
|
491
|
+
layout
|
|
492
|
+
}, byteLength) => {
|
|
455
493
|
if (warned.has(code)) {
|
|
456
494
|
return;
|
|
457
495
|
}
|
|
458
496
|
warned.add(code);
|
|
459
|
-
const message = warningText[code](byteLength);
|
|
497
|
+
const message = warningText[code](byteLength, layout);
|
|
460
498
|
const runtime = globalThis.process;
|
|
461
499
|
if (runtime && typeof runtime.emitWarning === 'function' && nodeLike()) {
|
|
462
500
|
runtime.emitWarning(message, {
|
package/dist/worker-thread.mjs
CHANGED
|
@@ -129,8 +129,8 @@ const respond = async ({
|
|
|
129
129
|
}, post) => {
|
|
130
130
|
const controller = check ? alreadyAborted() : controllers.get(id);
|
|
131
131
|
try {
|
|
132
|
-
// The call before this one may have run in one piece (
|
|
133
|
-
// of this call to arrive; take one before starting, so a call cancelled while it waited never runs.
|
|
132
|
+
// The call before this one may have run in one piece (a call without a signal does), with no turn of the event
|
|
133
|
+
// loop for a cancel of this call to arrive; take one before starting, so a call cancelled while it waited never runs.
|
|
134
134
|
if (controller && !controller.signal.aborted) {
|
|
135
135
|
await nextTurn();
|
|
136
136
|
}
|
package/dist/worker.cjs
CHANGED
|
@@ -471,7 +471,7 @@ const initOptions = (options, allowScript) => {
|
|
|
471
471
|
|
|
472
472
|
// A signal and a progress callback can't cross to the worker, so they stay on this side: the options are sent without
|
|
473
473
|
// them, a cancel message is sent when the signal aborts, and the worker's progress messages call the callback.
|
|
474
|
-
const takeControls = (options,
|
|
474
|
+
const takeControls = (options, ditherAllowed = true) => {
|
|
475
475
|
// Anything but an ordinary object goes on as it is, for the worker's own validation to reject. An ordinary one is copied
|
|
476
476
|
// once first: structured cloning drops inherited properties, and the copy is what gets validated and sent.
|
|
477
477
|
if (!(0, _shared.isOptionsObject)(options)) {
|
|
@@ -504,7 +504,7 @@ const takeControls = (options, progressAllowed = true, ditherAllowed = true) =>
|
|
|
504
504
|
options.dither = copy;
|
|
505
505
|
}
|
|
506
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
|
|
507
|
+
// A wrong one crosses as `null` instead, which the worker's validation
|
|
508
508
|
// rejects with the main entry's error, after the image and k: the main entry checks the options last. (This relies on
|
|
509
509
|
// those messages not echoing the value: if they ever say what they got, send a stand-in that reads the same.)
|
|
510
510
|
const {
|
|
@@ -512,7 +512,7 @@ const takeControls = (options, progressAllowed = true, ditherAllowed = true) =>
|
|
|
512
512
|
onProgress
|
|
513
513
|
} = options;
|
|
514
514
|
const badSignal = signal !== undefined && (signal === null || typeof signal !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function');
|
|
515
|
-
const badProgress = onProgress !== undefined &&
|
|
515
|
+
const badProgress = onProgress !== undefined && typeof onProgress !== 'function';
|
|
516
516
|
const {
|
|
517
517
|
signal: _signal,
|
|
518
518
|
onProgress: _onProgress,
|
|
@@ -577,10 +577,8 @@ const optionsPosition = {
|
|
|
577
577
|
const abortReason = signal => signal.reason !== undefined ? signal.reason : new DOMException('This operation was aborted', 'AbortError');
|
|
578
578
|
const imageFunctions = ['getClustering', 'getPalette', 'getSwatches', 'quantize', 'quantizeFromClustering', 'quantizeWithPalette', 'quantizeToIndices', 'quantizeToIndicesFromClustering', 'quantizeToIndicesWithPalette'];
|
|
579
579
|
const mappedImageFunctions = ['quantize', 'quantizeFromClustering', 'quantizeWithPalette'];
|
|
580
|
-
// These only map pixels, so they take a signal but no progress callback (see mapOptions in index.js).
|
|
581
580
|
// The functions that only cluster, and so take no dither option.
|
|
582
581
|
const clusteringOnlyFunctions = ['getClustering', 'getPalette', 'getSwatches'];
|
|
583
|
-
const mappingOnlyFunctions = ['quantizeFromClustering', 'quantizeWithPalette', 'quantizeToIndicesFromClustering', 'quantizeToIndicesWithPalette'];
|
|
584
582
|
const otherFunctions = ['getPaletteFromClustering', 'getSwatchesFromClustering', 'getClusteringInfo'];
|
|
585
583
|
|
|
586
584
|
// Calls each worker's `abandon` once the worker made by createWorker() has been garbage-collected (see `abandoned`).
|
|
@@ -888,7 +886,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
888
886
|
options,
|
|
889
887
|
signal,
|
|
890
888
|
onProgress
|
|
891
|
-
} = takeControls(rest[position], !
|
|
889
|
+
} = takeControls(rest[position], !clusteringOnlyFunctions.includes(name));
|
|
892
890
|
let args = rest.slice();
|
|
893
891
|
if (position < args.length) {
|
|
894
892
|
args[position] = options;
|
|
@@ -907,10 +905,10 @@ const makeWorker = (options, allowScript) => {
|
|
|
907
905
|
}
|
|
908
906
|
throw abortReason(signal);
|
|
909
907
|
}
|
|
910
|
-
// The
|
|
908
|
+
// The bare-image warning is decided here, on the caller's side and before a transfer can empty the
|
|
911
909
|
// array, and given once the call succeeds: the thread checks the other arguments, and its own copy is
|
|
912
910
|
// silenced (PRINCIPLES "Warnings").
|
|
913
|
-
const warning = (0, _shared.
|
|
911
|
+
const warning = (0, _shared.bareImageWarning)(image);
|
|
914
912
|
const byteLength = warning ? image.byteLength : 0;
|
|
915
913
|
let sent = send(image);
|
|
916
914
|
if (!ready()) {
|
|
@@ -968,12 +966,12 @@ const makeWorker = (options, allowScript) => {
|
|
|
968
966
|
options,
|
|
969
967
|
signal,
|
|
970
968
|
onProgress
|
|
971
|
-
} = takeControls(clusterOptions,
|
|
969
|
+
} = takeControls(clusterOptions, false);
|
|
972
970
|
if (signal && signal.aborted) {
|
|
973
971
|
throw abortReason(signal);
|
|
974
972
|
}
|
|
975
973
|
const id = nextHandle++;
|
|
976
|
-
const warning = (0, _shared.
|
|
974
|
+
const warning = (0, _shared.bareImageWarning)(image);
|
|
977
975
|
const byteLength = warning ? image.byteLength : 0;
|
|
978
976
|
const sent = ready() ? send(image) : take(send(image));
|
|
979
977
|
// Counted from before the call, so an abandoned worker isn't stopped between the call settling and the handle
|
|
@@ -1011,7 +1009,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
1011
1009
|
const withControls = (method, leading, mapOptions) => {
|
|
1012
1010
|
let taken;
|
|
1013
1011
|
try {
|
|
1014
|
-
taken = takeControls(mapOptions
|
|
1012
|
+
taken = takeControls(mapOptions);
|
|
1015
1013
|
} catch (error) {
|
|
1016
1014
|
return Promise.reject(error);
|
|
1017
1015
|
}
|
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, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge,
|
|
7
|
+
import { fail, isCodedError, detachedError, isOptionsObject, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge, bareImageWarning, warnOnce } 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";
|
|
@@ -402,7 +402,7 @@ const initOptions = (options, allowScript) => {
|
|
|
402
402
|
|
|
403
403
|
// A signal and a progress callback can't cross to the worker, so they stay on this side: the options are sent without
|
|
404
404
|
// them, a cancel message is sent when the signal aborts, and the worker's progress messages call the callback.
|
|
405
|
-
const takeControls = (options,
|
|
405
|
+
const takeControls = (options, ditherAllowed = true) => {
|
|
406
406
|
// Anything but an ordinary object goes on as it is, for the worker's own validation to reject. An ordinary one is copied
|
|
407
407
|
// once first: structured cloning drops inherited properties, and the copy is what gets validated and sent.
|
|
408
408
|
if (!isOptionsObject(options)) {
|
|
@@ -435,7 +435,7 @@ const takeControls = (options, progressAllowed = true, ditherAllowed = true) =>
|
|
|
435
435
|
options.dither = copy;
|
|
436
436
|
}
|
|
437
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
|
|
438
|
+
// A wrong one crosses as `null` instead, which the worker's validation
|
|
439
439
|
// rejects with the main entry's error, after the image and k: the main entry checks the options last. (This relies on
|
|
440
440
|
// those messages not echoing the value: if they ever say what they got, send a stand-in that reads the same.)
|
|
441
441
|
const {
|
|
@@ -443,7 +443,7 @@ const takeControls = (options, progressAllowed = true, ditherAllowed = true) =>
|
|
|
443
443
|
onProgress
|
|
444
444
|
} = options;
|
|
445
445
|
const badSignal = signal !== undefined && (signal === null || typeof signal !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function');
|
|
446
|
-
const badProgress = onProgress !== undefined &&
|
|
446
|
+
const badProgress = onProgress !== undefined && typeof onProgress !== 'function';
|
|
447
447
|
const {
|
|
448
448
|
signal: _signal,
|
|
449
449
|
onProgress: _onProgress,
|
|
@@ -508,10 +508,8 @@ const optionsPosition = {
|
|
|
508
508
|
const abortReason = signal => signal.reason !== undefined ? signal.reason : new DOMException('This operation was aborted', 'AbortError');
|
|
509
509
|
const imageFunctions = ['getClustering', 'getPalette', 'getSwatches', 'quantize', 'quantizeFromClustering', 'quantizeWithPalette', 'quantizeToIndices', 'quantizeToIndicesFromClustering', 'quantizeToIndicesWithPalette'];
|
|
510
510
|
const mappedImageFunctions = ['quantize', 'quantizeFromClustering', 'quantizeWithPalette'];
|
|
511
|
-
// These only map pixels, so they take a signal but no progress callback (see mapOptions in index.js).
|
|
512
511
|
// The functions that only cluster, and so take no dither option.
|
|
513
512
|
const clusteringOnlyFunctions = ['getClustering', 'getPalette', 'getSwatches'];
|
|
514
|
-
const mappingOnlyFunctions = ['quantizeFromClustering', 'quantizeWithPalette', 'quantizeToIndicesFromClustering', 'quantizeToIndicesWithPalette'];
|
|
515
513
|
const otherFunctions = ['getPaletteFromClustering', 'getSwatchesFromClustering', 'getClusteringInfo'];
|
|
516
514
|
|
|
517
515
|
// Calls each worker's `abandon` once the worker made by createWorker() has been garbage-collected (see `abandoned`).
|
|
@@ -819,7 +817,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
819
817
|
options,
|
|
820
818
|
signal,
|
|
821
819
|
onProgress
|
|
822
|
-
} = takeControls(rest[position], !
|
|
820
|
+
} = takeControls(rest[position], !clusteringOnlyFunctions.includes(name));
|
|
823
821
|
let args = rest.slice();
|
|
824
822
|
if (position < args.length) {
|
|
825
823
|
args[position] = options;
|
|
@@ -838,10 +836,10 @@ const makeWorker = (options, allowScript) => {
|
|
|
838
836
|
}
|
|
839
837
|
throw abortReason(signal);
|
|
840
838
|
}
|
|
841
|
-
// The
|
|
839
|
+
// The bare-image warning is decided here, on the caller's side and before a transfer can empty the
|
|
842
840
|
// array, and given once the call succeeds: the thread checks the other arguments, and its own copy is
|
|
843
841
|
// silenced (PRINCIPLES "Warnings").
|
|
844
|
-
const warning =
|
|
842
|
+
const warning = bareImageWarning(image);
|
|
845
843
|
const byteLength = warning ? image.byteLength : 0;
|
|
846
844
|
let sent = send(image);
|
|
847
845
|
if (!ready()) {
|
|
@@ -899,12 +897,12 @@ const makeWorker = (options, allowScript) => {
|
|
|
899
897
|
options,
|
|
900
898
|
signal,
|
|
901
899
|
onProgress
|
|
902
|
-
} = takeControls(clusterOptions,
|
|
900
|
+
} = takeControls(clusterOptions, false);
|
|
903
901
|
if (signal && signal.aborted) {
|
|
904
902
|
throw abortReason(signal);
|
|
905
903
|
}
|
|
906
904
|
const id = nextHandle++;
|
|
907
|
-
const warning =
|
|
905
|
+
const warning = bareImageWarning(image);
|
|
908
906
|
const byteLength = warning ? image.byteLength : 0;
|
|
909
907
|
const sent = ready() ? send(image) : take(send(image));
|
|
910
908
|
// Counted from before the call, so an abandoned worker isn't stopped between the call settling and the handle
|
|
@@ -942,7 +940,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
942
940
|
const withControls = (method, leading, mapOptions) => {
|
|
943
941
|
let taken;
|
|
944
942
|
try {
|
|
945
|
-
taken = takeControls(mapOptions
|
|
943
|
+
taken = takeControls(mapOptions);
|
|
946
944
|
} catch (error) {
|
|
947
945
|
return Promise.reject(error);
|
|
948
946
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agglomerative-clustering",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.7.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",
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"README.md"
|
|
82
82
|
],
|
|
83
83
|
"scripts": {
|
|
84
|
-
"build:wasm": "node scripts/check-emsdk.mjs && mkdirp dist && em++ src/clustering.cpp -O3 -msimd128 -s WASM=1 -s MODULARIZE=1 -s EXPORT_ES6=1 -s EXPORT_NAME='createWasmModule' -s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=32MB -s MAXIMUM_MEMORY=2147483648 -s EXPORTED_FUNCTIONS=\"['_library_version','_get_clustering','_clustering_job_start','_clustering_job_step','_clustering_job_result','_clustering_job_free','_get_palette','_get_palette_from_clustering','_get_palette_weights','_quantize','_quantize_with_clustering','_quantize_with_palette','_quantize_indices','_quantize_indices_with_clustering','_quantize_indices_with_palette','_malloc','_free']\" -s EXPORTED_RUNTIME_METHODS=\"['HEAPU8']\" -o dist/clustering.js && node scripts/patch-glue.mjs",
|
|
84
|
+
"build:wasm": "node scripts/check-emsdk.mjs && mkdirp dist && em++ src/clustering.cpp -O3 -msimd128 -s WASM=1 -s MODULARIZE=1 -s EXPORT_ES6=1 -s EXPORT_NAME='createWasmModule' -s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=32MB -s MAXIMUM_MEMORY=2147483648 -s EXPORTED_FUNCTIONS=\"['_library_version','_get_clustering','_clustering_job_start','_clustering_job_step','_clustering_job_result','_clustering_job_free','_mapping_job_start','_mapping_job_step','_mapping_job_result','_mapping_job_free','_get_palette','_get_palette_from_clustering','_get_palette_weights','_quantize','_quantize_with_clustering','_quantize_with_palette','_quantize_indices','_quantize_indices_with_clustering','_quantize_indices_with_palette','_malloc','_free']\" -s EXPORTED_RUNTIME_METHODS=\"['HEAPU8']\" -o dist/clustering.js && node scripts/patch-glue.mjs",
|
|
85
85
|
"build:cjs": "cross-env BABEL_ENV=cjs babel src --out-dir dist --extensions \".js\" --ignore \"src/worker-thread.js\" --out-file-extension .cjs && node scripts/patch-cjs.mjs",
|
|
86
86
|
"build:esm": "cross-env BABEL_ENV=esm babel src --out-dir dist --extensions \".js\" --out-file-extension .mjs",
|
|
87
87
|
"build:types": "mkdirp dist && node -e \"const fs = require('fs'); for (const f of ['index.d.ts', 'worker.d.ts']) {fs.copyFileSync('src/' + f, 'dist/' + f);} fs.copyFileSync('src/index.d.ts', 'dist/index.d.cts'); fs.writeFileSync('dist/worker.d.cts', fs.readFileSync('src/worker.d.ts', 'utf8').replaceAll(\\\"'./index.js'\\\", \\\"'./index.cjs'\\\"));\"",
|