agglomerative-clustering 4.0.0-beta.2 → 4.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +40 -238
- package/dist/index.d.cts +15 -76
- package/dist/index.d.ts +15 -76
- package/dist/index.mjs +28 -221
- package/dist/shared.cjs +244 -84
- package/dist/shared.mjs +235 -78
- package/dist/worker-thread.mjs +16 -19
- package/dist/worker.cjs +235 -136
- package/dist/worker.d.cts +0 -32
- package/dist/worker.d.ts +0 -32
- package/dist/worker.mjs +234 -130
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import createWasmModule from './clustering.js';
|
|
2
2
|
import { choose } from "./choose.mjs";
|
|
3
|
-
import { parseDither, version, fail, colorSpaceCodes, colorSpaces, colorSpaceName, isUint8Array, isUint8ClampedArray, isArrayBufferLike, isOptionsObject, snapshot, isPalette, isClustering, markPalette, markClustering, setEngine, imageHint, latestVersion, imageFormats, channels, hasAlpha, formatNames, detachedError, describe, readHeader, checkPalette, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, sizeText, tooLarge, sniffImageFormat,
|
|
3
|
+
import { parseDither, checkK, parseOptions, alphaModeCodes, clusterOptions, quantizeOptions, mapOptions, quantizeIndexOptions, mapIndexOptions, version, fail, colorSpaceCodes, colorSpaces, colorSpaceName, isUint8Array, isUint8ClampedArray, isArrayBufferLike, isOptionsObject, snapshot, isPalette, isClustering, markPalette, markClustering, setEngine, imageHint, latestVersion, imageFormats, channels, hasAlpha, formatNames, detachedError, describe, readHeader, checkPalette, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, sizeText, tooLarge, sniffImageFormat, bareImageError, checkImageBody, imageCopy, checkSize, checkPixelArray, checkNotEmpty, checkChoice } from "./shared.mjs";
|
|
4
4
|
|
|
5
5
|
// The library's version, the stored-data format version, and the Palette and Clustering types (see shared.js).
|
|
6
6
|
export { version, Palette, Clustering, errorCodes, isCodedError, ditherMethods, ditherSteps, colorSpaces, imageFormats, alphaModes } from "./shared.mjs";
|
|
@@ -21,10 +21,6 @@ const codes = {
|
|
|
21
21
|
'rgbx': 3,
|
|
22
22
|
'bgrx': 4
|
|
23
23
|
};
|
|
24
|
-
const alphaModeCodes = {
|
|
25
|
-
'ignore': 0,
|
|
26
|
-
'weight': 1
|
|
27
|
-
};
|
|
28
24
|
let Module = null;
|
|
29
25
|
|
|
30
26
|
// Turns init's `wasm` option into the settings the Emscripten glue understands. The glue finds its .wasm file next to
|
|
@@ -90,7 +86,7 @@ const hasSimd = () => {
|
|
|
90
86
|
}
|
|
91
87
|
};
|
|
92
88
|
// Node itself (or Bun), not a runtime that imitates it: Cloudflare Workers with nodejs_compat and Deno also define
|
|
93
|
-
// process.versions.node, but their navigator.userAgent says what they are (Node
|
|
89
|
+
// process.versions.node, but their navigator.userAgent says what they are (old Node versions have no navigator at all).
|
|
94
90
|
const isNodeLike = !!(globalThis.process && globalThis.process.versions && globalThis.process.versions.node) && (typeof navigator === 'undefined' || /^(Node\.js|Bun)\//.test(String(navigator.userAgent)));
|
|
95
91
|
// A browser page or worker, as opposed to a server-side runtime that isn't Node (Cloudflare Workers, Vercel's edge runtime,
|
|
96
92
|
// Deno Deploy): only those have a document, a worker scope or a browser's user agent.
|
|
@@ -404,35 +400,18 @@ const bytes = data => isUint8Array(data) ? data : new Uint8Array(data.buffer, da
|
|
|
404
400
|
// or change the array as soon as the call returns. Once the module is loaded the bytes go into its memory before the
|
|
405
401
|
// call returns; until then (the first call) a copy is kept for when it has loaded.
|
|
406
402
|
const held = data => Module ? data : data.slice();
|
|
407
|
-
|
|
408
|
-
// The bare-image warning (rgb-as-rgba, gray-as-rgba) for an image, decided before load() and given only once the call's own checks have passed,
|
|
409
|
-
// just before it starts work, so the one warning isn't spent on a call that fails anyway (PRINCIPLES "Warnings").
|
|
410
|
-
const noWarning = () => {};
|
|
411
|
-
// A call whose signal is already aborted fails before any work, so it doesn't use the warning up either.
|
|
412
|
-
const warningFor = image => {
|
|
413
|
-
const warning = bareImageWarning(image);
|
|
414
|
-
return warning ? signal => {
|
|
415
|
-
if (!(signal && signal.aborted)) {
|
|
416
|
-
warnOnce(warning, image.byteLength);
|
|
417
|
-
}
|
|
418
|
-
} : noWarning;
|
|
419
|
-
};
|
|
420
403
|
const load = image => {
|
|
421
|
-
if (
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
throw detachedError('bare');
|
|
404
|
+
if (ArrayBuffer.isView(image)) {
|
|
405
|
+
// What a bare typed array must be (see checkPixelArray, which the worker entry runs too); a detached one is reported first.
|
|
406
|
+
checkPixelArray(image);
|
|
407
|
+
if (isDetached(image)) {
|
|
408
|
+
throw detachedError('bare');
|
|
409
|
+
}
|
|
428
410
|
}
|
|
429
411
|
if (isUint8Array(image)) {
|
|
430
|
-
const
|
|
431
|
-
if (
|
|
432
|
-
throw
|
|
433
|
-
}
|
|
434
|
-
if (image.length % 4 !== 0) {
|
|
435
|
-
throw fail('invalid-argument', "Invalid image data: length must be a multiple of 4 for 'rgba'.", 'image');
|
|
412
|
+
const bare = bareImageError(image);
|
|
413
|
+
if (bare) {
|
|
414
|
+
throw bare;
|
|
436
415
|
}
|
|
437
416
|
return {
|
|
438
417
|
data: held(image),
|
|
@@ -452,9 +431,6 @@ const load = image => {
|
|
|
452
431
|
premultiplied: false
|
|
453
432
|
};
|
|
454
433
|
}
|
|
455
|
-
if (ArrayBuffer.isView(image)) {
|
|
456
|
-
throw fail('invalid-argument', "Invalid image: a bare typed array must be a Uint8Array of RGBA bytes. For a Uint8ClampedArray such as imageData.data, pass the ImageData itself or { data, format: 'rgba' }.", 'image');
|
|
457
|
-
}
|
|
458
434
|
if (typeof image !== 'object' || !image || !image.data || !image.format) {
|
|
459
435
|
throw fail('invalid-argument', `Invalid image: expected object with \`data\` and \`format\`.${imageHint(image)}`, 'image');
|
|
460
436
|
}
|
|
@@ -532,141 +508,6 @@ const unpackIndices = (output, image, colorSpace) => {
|
|
|
532
508
|
return result;
|
|
533
509
|
};
|
|
534
510
|
|
|
535
|
-
// Returns k, validated. Any k above the number of colours means all of them, so it is capped at the largest value
|
|
536
|
-
// the WASM side holds (a larger one would overflow its int).
|
|
537
|
-
const check = k => {
|
|
538
|
-
if (typeof k !== 'number' || !Number.isInteger(k) || k <= 0) {
|
|
539
|
-
// A string is the usual slip (an input's `value`), so it gets its own fix.
|
|
540
|
-
const got = typeof k === 'string' ? ` Got the string ${JSON.stringify(k.length > 20 ? `${k.slice(0, 20)}…` : k)}: convert it first, e.g. Number(input.value).` : typeof k === 'number' ? ` Got ${k}.` : ` Got ${k === null ? 'null' : typeof k}.`;
|
|
541
|
-
throw fail('invalid-argument', `Invalid k: must be a whole number of colours, 1 or more.${got}`, 'k');
|
|
542
|
-
}
|
|
543
|
-
return Math.min(k, 0x7FFFFFFF);
|
|
544
|
-
};
|
|
545
|
-
|
|
546
|
-
// Validates `options` against the keys a function accepts and fills in defaults:
|
|
547
|
-
// colorSpace: 'rgb' (default) or 'oklab', the space colours are clustered and matched in; when mapping with a
|
|
548
|
-
// clustering, the default is the space the clustering was made in (code -1)
|
|
549
|
-
// alpha: 'weight' (default) counts RGBA pixels in proportion to their opacity; 'ignore' counts every pixel
|
|
550
|
-
// precision: bits per channel, 1-8 (default 8, exact); lower values group similar colours before clustering
|
|
551
|
-
// dither: false (default), true or a strength in [0, 1] (Floyd-Steinberg), a method name, or { method, strength, steps } (see parseDither)
|
|
552
|
-
// width: image width in pixels, needed to dither a Uint8Array or a RawImage without `width`
|
|
553
|
-
// signal, onProgress: an AbortSignal and a progress callback, which make the clustering and the mapping run in slices
|
|
554
|
-
// (see clusterInSlices and mapInSlices)
|
|
555
|
-
// transparent: false (default), true (cutoff 128) or a cutoff from 1 to 255: pixels with alpha below it are left out of clustering (and the rest count whole);
|
|
556
|
-
// indexed output also gives them their own entry, which counts towards k
|
|
557
|
-
|
|
558
|
-
// The settings of dither, which a caller may write as options of their own; naming one of these gets a pointer to the
|
|
559
|
-
// object form.
|
|
560
|
-
const ditherSettings = ['ditherMethod', 'ditherStrength', 'ditherSteps', 'method', 'strength', 'steps', 'mix'];
|
|
561
|
-
const parseOptions = (options, allowed, image) => {
|
|
562
|
-
if (options === undefined) {
|
|
563
|
-
options = {};
|
|
564
|
-
}
|
|
565
|
-
if (!isOptionsObject(options)) {
|
|
566
|
-
throw fail('invalid-argument', "Invalid options: expected an object.", 'options');
|
|
567
|
-
}
|
|
568
|
-
options = snapshot(options);
|
|
569
|
-
for (const key of Object.keys(options)) {
|
|
570
|
-
if (!allowed.includes(key)) {
|
|
571
|
-
const hint = allowed.includes('dither') && ditherSettings.includes(key) ? ' Dithering settings go inside the dither option: dither: { method, strength, steps }.' : '';
|
|
572
|
-
throw fail('invalid-argument', `Invalid option '${key}': expected one of ${allowed.join(', ')}.${hint}`, `options.${key}`);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
const colorSpace = options.colorSpace === undefined ? 'rgb' : options.colorSpace;
|
|
576
|
-
if (!colorSpaces.includes(colorSpace)) {
|
|
577
|
-
throw fail('invalid-argument', "Invalid colorSpace: must be 'rgb' or 'oklab'.", 'options.colorSpace');
|
|
578
|
-
}
|
|
579
|
-
const alpha = options.alpha === undefined ? 'weight' : options.alpha;
|
|
580
|
-
if (!Object.keys(alphaModeCodes).includes(alpha)) {
|
|
581
|
-
throw fail('invalid-argument', "Invalid alpha: must be 'weight' or 'ignore'.", 'options.alpha');
|
|
582
|
-
}
|
|
583
|
-
const precision = options.precision === undefined ? 8 : options.precision;
|
|
584
|
-
if (typeof precision !== 'number' || !Number.isInteger(precision) || precision < 1 || precision > 8) {
|
|
585
|
-
throw fail('invalid-argument', "Invalid precision: must be an integer from 1 to 8.", 'options.precision');
|
|
586
|
-
}
|
|
587
|
-
const {
|
|
588
|
-
strength: dither,
|
|
589
|
-
method: ditherMethod,
|
|
590
|
-
steps: ditherSteps
|
|
591
|
-
} = parseDither(options.dither);
|
|
592
|
-
|
|
593
|
-
// The image's own width and options.width are two sources for one fact: they may repeat each other, but not disagree
|
|
594
|
-
// (a wrong row length would dither without an error, as long as it divides the pixel count).
|
|
595
|
-
if (options.width !== undefined && image && image.width !== undefined && options.width !== image.width) {
|
|
596
|
-
throw fail('invalid-argument', `Invalid width: options.width (${options.width}) does not match the image's width (${image.width}).`, 'options.width');
|
|
597
|
-
}
|
|
598
|
-
if (options.width !== undefined && image) {
|
|
599
|
-
checkSize(image.data.length / channels(image.format), options.width, 'width', 'options.width');
|
|
600
|
-
}
|
|
601
|
-
const width = options.width !== undefined ? options.width : image ? image.width : undefined;
|
|
602
|
-
if (dither > 0) {
|
|
603
|
-
const stride = channels(image.format);
|
|
604
|
-
if (typeof width !== 'number' || !Number.isInteger(width) || width <= 0) {
|
|
605
|
-
throw fail('invalid-argument', "Invalid width: dithering needs the image width, as `options.width` or the image's `width`.", 'options.width');
|
|
606
|
-
}
|
|
607
|
-
if (image.data.length / stride % width !== 0) {
|
|
608
|
-
throw fail('invalid-argument', "Invalid width: the image's pixel count is not a multiple of it.", 'options.width');
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
const signal = options.signal;
|
|
612
|
-
if (signal !== undefined && (signal === null || typeof signal !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function')) {
|
|
613
|
-
throw fail('invalid-argument', "Invalid signal: must be an AbortSignal.", 'options.signal');
|
|
614
|
-
}
|
|
615
|
-
const onProgress = options.onProgress;
|
|
616
|
-
if (onProgress !== undefined && typeof onProgress !== 'function') {
|
|
617
|
-
throw fail('invalid-argument', "Invalid onProgress: must be a function.", 'options.onProgress');
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
// The alpha cutoff for indexed output: 0 for none, else the pixels with alpha below it are transparent.
|
|
621
|
-
const transparentOption = options.transparent === undefined ? false : options.transparent;
|
|
622
|
-
const transparent = transparentOption === false ? 0 : transparentOption === true ? 128 : transparentOption;
|
|
623
|
-
if (!Number.isInteger(transparent) || transparent < 0 || transparent > 255 || transparent === 0 && transparentOption !== false) {
|
|
624
|
-
throw fail('invalid-argument', "Invalid transparent: must be a boolean or an integer alpha cutoff from 1 to 255.", 'options.transparent');
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
// A cutoff replaces opacity weighting (pixels count whole or not at all), so naming both would leave one of them ignored.
|
|
628
|
-
if (transparent > 0 && options.alpha !== undefined) {
|
|
629
|
-
throw fail('invalid-argument', "Invalid options: `transparent` replaces `alpha` (with a cutoff, pixels count whole or not at all), so give one or the other.", 'options');
|
|
630
|
-
}
|
|
631
|
-
return {
|
|
632
|
-
colorSpace: colorSpaceCodes[colorSpace],
|
|
633
|
-
colorSpaceGiven: options.colorSpace !== undefined,
|
|
634
|
-
alpha: alphaModeCodes[alpha],
|
|
635
|
-
precision,
|
|
636
|
-
dither,
|
|
637
|
-
ditherMethod,
|
|
638
|
-
ditherSteps,
|
|
639
|
-
width: dither > 0 ? width : 0,
|
|
640
|
-
transparent,
|
|
641
|
-
transparentGiven: options.transparent !== undefined,
|
|
642
|
-
signal,
|
|
643
|
-
onProgress
|
|
644
|
-
};
|
|
645
|
-
};
|
|
646
|
-
const controlOptions = ['signal', 'onProgress'];
|
|
647
|
-
const clusterOptions = ['colorSpace', 'precision', 'alpha', 'transparent', ...controlOptions];
|
|
648
|
-
const quantizeOptions = [...clusterOptions, 'dither', 'width'];
|
|
649
|
-
const mapOptions = ['colorSpace', 'dither', 'width', ...controlOptions];
|
|
650
|
-
const quantizeIndexOptions = quantizeOptions;
|
|
651
|
-
const mapIndexOptions = [...mapOptions, 'transparent'];
|
|
652
|
-
|
|
653
|
-
// An empty palette is what getPalette gives for an image with no visible pixels, and mapping such an image with it is
|
|
654
|
-
// fine. Mapping visible pixels to it has no answer (they would all come out black), so that is an error. `cutoff` is the
|
|
655
|
-
// alpha from which an RGBA pixel needs a colour.
|
|
656
|
-
const checkNotEmpty = (palette, image, cutoff) => {
|
|
657
|
-
if (palette.length > 0) {
|
|
658
|
-
return;
|
|
659
|
-
}
|
|
660
|
-
const data = image.data;
|
|
661
|
-
let visible = hasAlpha(image.format) ? false : data.length > 0;
|
|
662
|
-
for (let i = 3; !visible && hasAlpha(image.format) && i < data.length; i += 4) {
|
|
663
|
-
visible = data[i] >= cutoff;
|
|
664
|
-
}
|
|
665
|
-
if (visible) {
|
|
666
|
-
throw fail('invalid-argument', "Invalid palette: it is empty, but the image has visible pixels to map to it.", 'palette');
|
|
667
|
-
}
|
|
668
|
-
};
|
|
669
|
-
|
|
670
511
|
// The space to match pixels to a palette in: the one the caller names, else the one a Palette was made in, else RGB.
|
|
671
512
|
const matchingSpace = (o, palette) => o.colorSpaceGiven ? o.colorSpace : colorSpaceCodes[isPalette(palette) ? palette.colorSpace : 'rgb'];
|
|
672
513
|
|
|
@@ -924,12 +765,11 @@ const mapInSlices = async (wasm, image, source, colorSpace, indices, cutoff, o,
|
|
|
924
765
|
|
|
925
766
|
// The functions that read a clustering take no options: a clustering already says how it was made. An options object passed
|
|
926
767
|
// anyway (say { colorSpace: 'oklab' }, expecting it to apply) would be silently ignored, so it is an error. undefined is fine.
|
|
927
|
-
// `name` is how the caller wrote the call
|
|
928
|
-
//
|
|
768
|
+
// `name` is how the caller wrote the call (the method, `clustering.getPalette`), whose message points at the method form
|
|
769
|
+
// of mapping.
|
|
929
770
|
const noOptions = (name, extra) => {
|
|
930
771
|
if (extra.some(value => value !== undefined)) {
|
|
931
|
-
|
|
932
|
-
throw fail('invalid-argument', `Invalid arguments: ${name} takes no options (a clustering already records its colour space, precision and cutoff). To map pixels with other options, use ${alternative}.`, 'options');
|
|
772
|
+
throw fail('invalid-argument', `Invalid arguments: ${name} takes no options (a clustering already records its colour space, precision and cutoff). To map pixels with other options, use clustering.quantize(image, k, options).`, 'options');
|
|
933
773
|
}
|
|
934
774
|
};
|
|
935
775
|
|
|
@@ -939,10 +779,8 @@ const noOptions = (name, extra) => {
|
|
|
939
779
|
|
|
940
780
|
export const buildClustering = async (image, options) => {
|
|
941
781
|
image = imageCopy(image);
|
|
942
|
-
const warn = warningFor(image);
|
|
943
782
|
image = load(image);
|
|
944
783
|
const o = parseOptions(options, clusterOptions, image);
|
|
945
|
-
warn(o.signal);
|
|
946
784
|
const wasm = Module || (await acquire());
|
|
947
785
|
if (isControlled(o)) {
|
|
948
786
|
const report = reporter(o);
|
|
@@ -952,15 +790,11 @@ export const buildClustering = async (image, options) => {
|
|
|
952
790
|
}
|
|
953
791
|
return markClustering(call(wasm, [image.data], imagePointer => wasm._get_clustering(imagePointer, image.data.length, codes[image.format], o.colorSpace, o.precision, o.alpha, o.transparent), o.precision, image.premultiplied));
|
|
954
792
|
};
|
|
955
|
-
/** @deprecated The same function as buildClustering, its name before 3.4.0. Removed in 4.0. */
|
|
956
|
-
export const getClustering = buildClustering;
|
|
957
793
|
export const buildPalette = async (image, k, options) => {
|
|
958
794
|
image = imageCopy(image);
|
|
959
|
-
const warn = warningFor(image);
|
|
960
795
|
image = load(image);
|
|
961
|
-
k =
|
|
796
|
+
k = checkK(k);
|
|
962
797
|
const o = parseOptions(options, clusterOptions, image);
|
|
963
|
-
warn(o.signal);
|
|
964
798
|
const wasm = Module || (await acquire());
|
|
965
799
|
if (isControlled(o)) {
|
|
966
800
|
const report = reporter(o);
|
|
@@ -972,19 +806,16 @@ export const buildPalette = async (image, k, options) => {
|
|
|
972
806
|
}
|
|
973
807
|
return markPalette(call(wasm, [image.data], imagePointer => wasm._get_palette(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.transparent), o.precision, image.premultiplied), colorSpaceName(o.colorSpace));
|
|
974
808
|
};
|
|
975
|
-
/** @deprecated The same function as buildPalette, its name before 3.4.0. Removed in 4.0. */
|
|
976
|
-
export const getPalette = buildPalette;
|
|
977
809
|
|
|
978
|
-
// The cuts take the name they were called by, for their messages: the
|
|
810
|
+
// The cuts take the name they were called by, for their messages: the methods on Clustering (or the function that cuts).
|
|
979
811
|
const cutPalette = async (name, clustering, k, extra) => {
|
|
980
812
|
const header = readHeader(clustering);
|
|
981
|
-
k =
|
|
813
|
+
k = checkK(k);
|
|
982
814
|
noOptions(name, extra);
|
|
983
815
|
clustering = held(clustering);
|
|
984
816
|
const wasm = Module || (await acquire());
|
|
985
817
|
return markPalette(call(wasm, [clustering], clusteringPointer => wasm._get_palette_from_clustering(clusteringPointer, clustering.length, k)), colorSpaceName(header.colorSpace));
|
|
986
818
|
};
|
|
987
|
-
export const getPaletteFromClustering = (clustering, k, ...extra) => cutPalette('getPaletteFromClustering', clustering, k, extra);
|
|
988
819
|
|
|
989
820
|
// The weight of each colour in the k-colour cut, in pixels (opacity-weighted), read from the clustering's records.
|
|
990
821
|
const weightsOf = (wasm, clustering, header, k) => {
|
|
@@ -1008,7 +839,7 @@ const needsWeights = header => {
|
|
|
1008
839
|
// proportion to their opacity, as in clustering. Shares are the weights divided by their total, so they sum to 1.
|
|
1009
840
|
const cutSwatches = async (name, clustering, k, extra) => {
|
|
1010
841
|
const header = readHeader(clustering);
|
|
1011
|
-
k =
|
|
842
|
+
k = checkK(k);
|
|
1012
843
|
noOptions(name, extra);
|
|
1013
844
|
needsWeights(header);
|
|
1014
845
|
clustering = held(clustering);
|
|
@@ -1031,19 +862,16 @@ const cutSwatches = async (name, clustering, k, extra) => {
|
|
|
1031
862
|
shares
|
|
1032
863
|
};
|
|
1033
864
|
};
|
|
1034
|
-
export const getSwatchesFromClustering = (clustering, k, ...extra) => cutSwatches('getSwatchesFromClustering', clustering, k, extra);
|
|
1035
865
|
|
|
1036
|
-
// The same for an image: clusters it, then cuts, like
|
|
866
|
+
// The same for an image: clusters it, then cuts, like buildPalette.
|
|
1037
867
|
export const buildSwatches = async (image, k, options) => {
|
|
1038
868
|
image = imageCopy(image);
|
|
1039
869
|
// Arguments are checked in the order they are given (image, then k, then options), as in every other function.
|
|
1040
870
|
// `load` only validates here (buildClustering loads again); both see the copy, so the caller's fields are read once.
|
|
1041
871
|
load(image);
|
|
1042
|
-
k =
|
|
1043
|
-
return
|
|
872
|
+
k = checkK(k);
|
|
873
|
+
return cutSwatches('buildSwatches', await buildClustering(image, options), k, []);
|
|
1044
874
|
};
|
|
1045
|
-
/** @deprecated The same function as buildSwatches, its name before 3.4.0. Removed in 4.0. */
|
|
1046
|
-
export const getSwatches = buildSwatches;
|
|
1047
875
|
|
|
1048
876
|
// What a clustering holds, read from its header and records without the WASM module. `mse[k - 1]` is the mean
|
|
1049
877
|
// squared error per channel of the k-colour clustering: each pixel's squared distance to its cluster's colour,
|
|
@@ -1091,15 +919,12 @@ const readInfo = async (name, clustering, extra) => {
|
|
|
1091
919
|
}
|
|
1092
920
|
return info;
|
|
1093
921
|
};
|
|
1094
|
-
export const getClusteringInfo = (clustering, ...extra) => readInfo('getClusteringInfo', clustering, extra);
|
|
1095
922
|
export const quantize = async (image, k, options) => {
|
|
1096
923
|
image = imageCopy(image);
|
|
1097
|
-
const warn = warningFor(image);
|
|
1098
924
|
const wrap = getWrapper(image);
|
|
1099
925
|
image = load(image);
|
|
1100
|
-
k =
|
|
926
|
+
k = checkK(k);
|
|
1101
927
|
const o = parseOptions(options, quantizeOptions, image);
|
|
1102
|
-
warn(o.signal);
|
|
1103
928
|
const wasm = Module || (await acquire());
|
|
1104
929
|
if (isControlled(o)) {
|
|
1105
930
|
const report = reporter(o);
|
|
@@ -1114,16 +939,14 @@ export const quantize = async (image, k, options) => {
|
|
|
1114
939
|
}
|
|
1115
940
|
return wrap(call(wasm, [image.data], imagePointer => wasm._quantize(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.dither, o.ditherMethod, o.ditherSteps, o.width, o.transparent), o.precision, image.premultiplied, true));
|
|
1116
941
|
};
|
|
1117
|
-
|
|
942
|
+
const quantizeFromClustering = async (image, clustering, k, options) => {
|
|
1118
943
|
image = imageCopy(image);
|
|
1119
|
-
const warn = warningFor(image);
|
|
1120
944
|
const wrap = getWrapper(image);
|
|
1121
945
|
image = load(image);
|
|
1122
946
|
readHeader(clustering);
|
|
1123
|
-
k =
|
|
947
|
+
k = checkK(k);
|
|
1124
948
|
const o = parseOptions(options, mapOptions, image);
|
|
1125
949
|
clustering = held(clustering);
|
|
1126
|
-
warn(o.signal);
|
|
1127
950
|
const wasm = Module || (await acquire());
|
|
1128
951
|
checkAborted(o.signal);
|
|
1129
952
|
const colorSpace = o.colorSpaceGiven ? o.colorSpace : -1;
|
|
@@ -1141,7 +964,6 @@ export const quantizeFromClustering = async (image, clustering, k, options) => {
|
|
|
1141
964
|
};
|
|
1142
965
|
export const quantizeWithPalette = async (image, palette, options) => {
|
|
1143
966
|
image = imageCopy(image);
|
|
1144
|
-
const warn = warningFor(image);
|
|
1145
967
|
const wrap = getWrapper(image);
|
|
1146
968
|
image = load(image);
|
|
1147
969
|
checkPalette(palette);
|
|
@@ -1149,7 +971,6 @@ export const quantizeWithPalette = async (image, palette, options) => {
|
|
|
1149
971
|
checkNotEmpty(palette, image, 1);
|
|
1150
972
|
const space = matchingSpace(o, palette);
|
|
1151
973
|
palette = held(palette);
|
|
1152
|
-
warn(o.signal);
|
|
1153
974
|
const wasm = Module || (await acquire());
|
|
1154
975
|
checkAborted(o.signal);
|
|
1155
976
|
if (isControlled(o)) {
|
|
@@ -1165,12 +986,10 @@ export const quantizeWithPalette = async (image, palette, options) => {
|
|
|
1165
986
|
};
|
|
1166
987
|
export const quantizeToIndices = async (image, k, options) => {
|
|
1167
988
|
image = imageCopy(image);
|
|
1168
|
-
const warn = warningFor(image);
|
|
1169
989
|
const original = image;
|
|
1170
990
|
image = load(image);
|
|
1171
|
-
k =
|
|
991
|
+
k = checkK(k);
|
|
1172
992
|
const o = parseOptions(options, quantizeIndexOptions, image);
|
|
1173
|
-
warn(o.signal);
|
|
1174
993
|
const wasm = Module || (await acquire());
|
|
1175
994
|
if (isControlled(o)) {
|
|
1176
995
|
const report = reporter(o);
|
|
@@ -1185,16 +1004,14 @@ export const quantizeToIndices = async (image, k, options) => {
|
|
|
1185
1004
|
}
|
|
1186
1005
|
return unpackIndices(call(wasm, [image.data], imagePointer => wasm._quantize_indices(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.dither, o.ditherMethod, o.ditherSteps, o.width, o.transparent), o.precision, image.premultiplied), original, colorSpaceName(o.colorSpace));
|
|
1187
1006
|
};
|
|
1188
|
-
|
|
1007
|
+
const quantizeToIndicesFromClustering = async (image, clustering, k, options) => {
|
|
1189
1008
|
image = imageCopy(image);
|
|
1190
|
-
const warn = warningFor(image);
|
|
1191
1009
|
const original = image;
|
|
1192
1010
|
image = load(image);
|
|
1193
1011
|
const header = readHeader(clustering);
|
|
1194
|
-
k =
|
|
1012
|
+
k = checkK(k);
|
|
1195
1013
|
const o = parseOptions(options, mapIndexOptions, image);
|
|
1196
1014
|
clustering = held(clustering);
|
|
1197
|
-
warn(o.signal);
|
|
1198
1015
|
const wasm = Module || (await acquire());
|
|
1199
1016
|
checkAborted(o.signal);
|
|
1200
1017
|
const colorSpace = o.colorSpaceGiven ? o.colorSpace : -1;
|
|
@@ -1215,7 +1032,6 @@ export const quantizeToIndicesFromClustering = async (image, clustering, k, opti
|
|
|
1215
1032
|
};
|
|
1216
1033
|
export const quantizeToIndicesWithPalette = async (image, palette, options) => {
|
|
1217
1034
|
image = imageCopy(image);
|
|
1218
|
-
const warn = warningFor(image);
|
|
1219
1035
|
const original = image;
|
|
1220
1036
|
image = load(image);
|
|
1221
1037
|
checkPalette(palette);
|
|
@@ -1224,7 +1040,6 @@ export const quantizeToIndicesWithPalette = async (image, palette, options) => {
|
|
|
1224
1040
|
const space = matchingSpace(o, palette);
|
|
1225
1041
|
const paletteSpace = isPalette(palette) ? palette.colorSpace : colorSpaceName(space);
|
|
1226
1042
|
palette = held(palette);
|
|
1227
|
-
warn(o.signal);
|
|
1228
1043
|
const wasm = Module || (await acquire());
|
|
1229
1044
|
checkAborted(o.signal);
|
|
1230
1045
|
if (isControlled(o)) {
|
|
@@ -1243,16 +1058,8 @@ export const quantizeToIndicesWithPalette = async (image, palette, options) => {
|
|
|
1243
1058
|
// at once), then runs the search in choose.js on this thread with the cuts above.
|
|
1244
1059
|
const chooseFrom = async (name, clustering, palette, k, options) => {
|
|
1245
1060
|
const header = readHeader(clustering);
|
|
1246
|
-
|
|
1247
|
-
if (palette.length === 0) {
|
|
1248
|
-
throw fail('invalid-argument', "Invalid palette: it is empty; give at least one RGB entry (3 bytes).", 'palette');
|
|
1249
|
-
}
|
|
1061
|
+
k = checkChoice(palette, k);
|
|
1250
1062
|
const entries = Uint8Array.from(palette);
|
|
1251
|
-
const count = entries.length / 3;
|
|
1252
|
-
k = check(k);
|
|
1253
|
-
if (k > count) {
|
|
1254
|
-
throw fail('invalid-argument', `Invalid k: ${k} is more than the ${count} entries in the palette; use ${count} or fewer.`, 'k');
|
|
1255
|
-
}
|
|
1256
1063
|
const o = parseOptions(options, ['colorSpace', 'signal']);
|
|
1257
1064
|
needsWeights(header);
|
|
1258
1065
|
clustering = held(clustering);
|