agglomerative-clustering 3.0.0-beta.7 → 3.1.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 +40 -841
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +61 -23
- package/dist/index.d.cts +53 -8
- package/dist/index.d.ts +53 -8
- package/dist/index.mjs +31 -24
- package/dist/shared.cjs +97 -5
- package/dist/shared.mjs +94 -3
- package/dist/worker-thread.mjs +20 -1
- package/dist/worker.cjs +40 -1
- package/dist/worker.d.cts +5 -2
- package/dist/worker.d.ts +5 -2
- package/dist/worker.mjs +10 -2
- package/package.json +1 -1
package/dist/clustering.wasm
CHANGED
|
Binary file
|
package/dist/index.cjs
CHANGED
|
@@ -15,13 +15,44 @@ Object.defineProperty(exports, "Palette", {
|
|
|
15
15
|
return _shared.Palette;
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
|
+
Object.defineProperty(exports, "alphaModes", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _shared.alphaModes;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "colorSpaces", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () {
|
|
27
|
+
return _shared.colorSpaces;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(exports, "ditherMethods", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () {
|
|
33
|
+
return _shared.ditherMethods;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, "ditherSteps", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () {
|
|
39
|
+
return _shared.ditherSteps;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
18
42
|
Object.defineProperty(exports, "errorCodes", {
|
|
19
43
|
enumerable: true,
|
|
20
44
|
get: function () {
|
|
21
45
|
return _shared.errorCodes;
|
|
22
46
|
}
|
|
23
47
|
});
|
|
24
|
-
exports.
|
|
48
|
+
exports.getSwatchesFromClustering = exports.getSwatches = exports.getPaletteFromClustering = exports.getPalette = exports.getClusteringInfo = exports.getClustering = exports.formatVersion = void 0;
|
|
49
|
+
Object.defineProperty(exports, "imageFormats", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
get: function () {
|
|
52
|
+
return _shared.imageFormats;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
exports.init = void 0;
|
|
25
56
|
Object.defineProperty(exports, "isCodedError", {
|
|
26
57
|
enumerable: true,
|
|
27
58
|
get: function () {
|
|
@@ -60,7 +91,7 @@ const codes = {
|
|
|
60
91
|
'rgba': 0,
|
|
61
92
|
'rgb': 1
|
|
62
93
|
};
|
|
63
|
-
const
|
|
94
|
+
const alphaModeCodes = {
|
|
64
95
|
'ignore': 0,
|
|
65
96
|
'weight': 1
|
|
66
97
|
};
|
|
@@ -153,7 +184,7 @@ const wasmLoadFailed = (cause, given) => {
|
|
|
153
184
|
let advice;
|
|
154
185
|
if (/disallowed by embedder|code generation disallowed/i.test(text) && !isNodeLike && !isBrowserLike()) {
|
|
155
186
|
// Edge runtimes (Cloudflare Workers, Vercel's edge runtime) compile WebAssembly only at build time, from an import.
|
|
156
|
-
advice = "This runtime doesn't compile WebAssembly while it runs, from bytes or from a URL (edge runtimes such as Cloudflare Workers and Vercel's): import clustering.wasm as a module, which the bundler compiles when it builds (in Wrangler a plain import of 'agglomerative-clustering/clustering.wasm'; in Next.js a relative path with ?module), and pass it to init({ wasm }) before the first call, as the
|
|
187
|
+
advice = "This runtime doesn't compile WebAssembly while it runs, from bytes or from a URL (edge runtimes such as Cloudflare Workers and Vercel's): import clustering.wasm as a module, which the bundler compiles when it builds (in Wrangler a plain import of 'agglomerative-clustering/clustering.wasm'; in Next.js a relative path with ?module), and pass it to init({ wasm }) before the first call, as the docs show for Cloudflare Workers and Next.js (https://github.com/RohanFredriksson/agglomerative-clustering/blob/master/docs/environments.md).";
|
|
157
188
|
} else if (/(memory|allocate)/i.test(text) && /(could not|cannot|can't|failed to|out of) (allocate|memory|reserve)/i.test(text)) {
|
|
158
189
|
// A 64-bit engine reserves the module's whole address range up front (about 10 GB of virtual memory per instance),
|
|
159
190
|
// though it only uses what it needs; an address-space limit refuses that reservation.
|
|
@@ -199,7 +230,7 @@ const wasmLoadFailed = (cause, given) => {
|
|
|
199
230
|
if (/Invalid URL/i.test(reason)) {
|
|
200
231
|
reason = "the package can't tell where clustering.wasm is in this runtime";
|
|
201
232
|
}
|
|
202
|
-
advice = "Give the module to init({ wasm }) before the first call: in an edge runtime (Cloudflare Workers, Vercel's), import clustering.wasm as a compiled module (the
|
|
233
|
+
advice = "Give the module to init({ wasm }) before the first call: in an edge runtime (Cloudflare Workers, Vercel's), import clustering.wasm as a compiled module (the docs show how for Cloudflare Workers and Next.js: https://github.com/RohanFredriksson/agglomerative-clustering/blob/master/docs/environments.md); elsewhere its bytes or URL.";
|
|
203
234
|
} else {
|
|
204
235
|
advice = "Check that clustering.wasm is served next to the script (not a 404, and with the application/wasm type) and allowed by the page's Content-Security-Policy, or say where it is with init({ wasm }).";
|
|
205
236
|
}
|
|
@@ -634,12 +665,15 @@ const check = k => {
|
|
|
634
665
|
// clustering, the default is the space the clustering was made in (code -1)
|
|
635
666
|
// alpha: 'weight' (default) counts RGBA pixels in proportion to their opacity; 'ignore' counts every pixel
|
|
636
667
|
// precision: bits per channel, 1-8 (default 8, exact); lower values group similar colours before clustering
|
|
637
|
-
// dither: false (default), true
|
|
668
|
+
// dither: false (default), true or a strength in [0, 1] (Floyd-Steinberg), a method name, or { method, strength, steps } (see parseDither)
|
|
638
669
|
// width: image width in pixels, needed to dither a Uint8Array or a RawImage without `width`
|
|
639
670
|
// signal, onProgress: an AbortSignal and a progress callback, which make the clustering run in slices (see clusterInSlices)
|
|
640
671
|
// 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);
|
|
641
672
|
// indexed output also gives them their own entry, which counts towards k
|
|
642
673
|
|
|
674
|
+
// The settings of dither, which a caller may write as options of their own; naming one of these gets a pointer to the
|
|
675
|
+
// object form.
|
|
676
|
+
const ditherSettings = ['ditherMethod', 'ditherStrength', 'ditherSteps', 'method', 'strength', 'steps', 'mix'];
|
|
643
677
|
const parseOptions = (options, allowed, image) => {
|
|
644
678
|
if (options === undefined) {
|
|
645
679
|
options = {};
|
|
@@ -650,25 +684,27 @@ const parseOptions = (options, allowed, image) => {
|
|
|
650
684
|
options = (0, _shared.snapshot)(options);
|
|
651
685
|
for (const key of Object.keys(options)) {
|
|
652
686
|
if (!allowed.includes(key)) {
|
|
653
|
-
|
|
687
|
+
const hint = allowed.includes('dither') && ditherSettings.includes(key) ? ' Dithering settings go inside the dither option: dither: { method, strength, steps }.' : '';
|
|
688
|
+
throw (0, _shared.fail)('invalid-argument', `Invalid option '${key}': expected one of ${allowed.join(', ')}.${hint}`, `options.${key}`);
|
|
654
689
|
}
|
|
655
690
|
}
|
|
656
691
|
const colorSpace = options.colorSpace === undefined ? 'rgb' : options.colorSpace;
|
|
657
|
-
if (!
|
|
692
|
+
if (!_shared.colorSpaces.includes(colorSpace)) {
|
|
658
693
|
throw (0, _shared.fail)('invalid-argument', "Invalid colorSpace: must be 'rgb' or 'oklab'.", 'options.colorSpace');
|
|
659
694
|
}
|
|
660
695
|
const alpha = options.alpha === undefined ? 'weight' : options.alpha;
|
|
661
|
-
if (!Object.keys(
|
|
696
|
+
if (!Object.keys(alphaModeCodes).includes(alpha)) {
|
|
662
697
|
throw (0, _shared.fail)('invalid-argument', "Invalid alpha: must be 'weight' or 'ignore'.", 'options.alpha');
|
|
663
698
|
}
|
|
664
699
|
const precision = options.precision === undefined ? 8 : options.precision;
|
|
665
700
|
if (typeof precision !== 'number' || !Number.isInteger(precision) || precision < 1 || precision > 8) {
|
|
666
701
|
throw (0, _shared.fail)('invalid-argument', "Invalid precision: must be an integer from 1 to 8.", 'options.precision');
|
|
667
702
|
}
|
|
668
|
-
const
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
703
|
+
const {
|
|
704
|
+
strength: dither,
|
|
705
|
+
method: ditherMethod,
|
|
706
|
+
steps: ditherSteps
|
|
707
|
+
} = (0, _shared.parseDither)(options.dither);
|
|
672
708
|
|
|
673
709
|
// The image's own width and options.width are two sources for one fact: they may repeat each other, but not disagree
|
|
674
710
|
// (a wrong row length would dither without an error, as long as it divides the pixel count).
|
|
@@ -709,11 +745,13 @@ const parseOptions = (options, allowed, image) => {
|
|
|
709
745
|
throw (0, _shared.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');
|
|
710
746
|
}
|
|
711
747
|
return {
|
|
712
|
-
colorSpace: _shared.
|
|
748
|
+
colorSpace: _shared.colorSpaceCodes[colorSpace],
|
|
713
749
|
colorSpaceGiven: options.colorSpace !== undefined,
|
|
714
|
-
alpha:
|
|
750
|
+
alpha: alphaModeCodes[alpha],
|
|
715
751
|
precision,
|
|
716
752
|
dither,
|
|
753
|
+
ditherMethod,
|
|
754
|
+
ditherSteps,
|
|
717
755
|
width: dither > 0 ? width : 0,
|
|
718
756
|
transparent,
|
|
719
757
|
transparentGiven: options.transparent !== undefined,
|
|
@@ -748,7 +786,7 @@ const checkNotEmpty = (palette, image, cutoff) => {
|
|
|
748
786
|
};
|
|
749
787
|
|
|
750
788
|
// The space to match pixels to a palette in: the one the caller names, else the one a Palette was made in, else RGB.
|
|
751
|
-
const matchingSpace = (o, palette) => o.colorSpaceGiven ? o.colorSpace : _shared.
|
|
789
|
+
const matchingSpace = (o, palette) => o.colorSpaceGiven ? o.colorSpace : _shared.colorSpaceCodes[(0, _shared.isPalette)(palette) ? palette.colorSpace : 'rgb'];
|
|
752
790
|
|
|
753
791
|
// Allocates `length` bytes in the WASM heap, or throws a coded out-of-memory error.
|
|
754
792
|
const allocate = (wasm, length) => {
|
|
@@ -1059,11 +1097,11 @@ const quantize = async (image, k, options) => {
|
|
|
1059
1097
|
const report = reporter(o);
|
|
1060
1098
|
const clustering = await clusterInSlices(wasm, image, o, report, 0.9);
|
|
1061
1099
|
checkAborted(o.signal);
|
|
1062
|
-
const mapped = wrap(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.width)));
|
|
1100
|
+
const mapped = wrap(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.ditherMethod, o.ditherSteps, o.width)));
|
|
1063
1101
|
report(1);
|
|
1064
1102
|
return mapped;
|
|
1065
1103
|
}
|
|
1066
|
-
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.width, o.transparent), o.precision));
|
|
1104
|
+
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));
|
|
1067
1105
|
};
|
|
1068
1106
|
exports.quantize = quantize;
|
|
1069
1107
|
const quantizeFromClustering = async (image, clustering, k, options) => {
|
|
@@ -1076,7 +1114,7 @@ const quantizeFromClustering = async (image, clustering, k, options) => {
|
|
|
1076
1114
|
const wasm = Module || (await acquire());
|
|
1077
1115
|
checkAborted(o.signal);
|
|
1078
1116
|
const colorSpace = o.colorSpaceGiven ? o.colorSpace : -1;
|
|
1079
|
-
const result = wrap(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.width)));
|
|
1117
|
+
const result = wrap(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.ditherMethod, o.ditherSteps, o.width)));
|
|
1080
1118
|
return result;
|
|
1081
1119
|
};
|
|
1082
1120
|
exports.quantizeFromClustering = quantizeFromClustering;
|
|
@@ -1090,7 +1128,7 @@ const quantizeWithPalette = async (image, palette, options) => {
|
|
|
1090
1128
|
palette = held(palette);
|
|
1091
1129
|
const wasm = Module || (await acquire());
|
|
1092
1130
|
checkAborted(o.signal);
|
|
1093
|
-
const result = wrap(call(wasm, [image.data, palette], (imagePointer, palettePointer) => wasm._quantize_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, space, o.dither, o.width)));
|
|
1131
|
+
const result = wrap(call(wasm, [image.data, palette], (imagePointer, palettePointer) => wasm._quantize_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, space, o.dither, o.ditherMethod, o.ditherSteps, o.width)));
|
|
1094
1132
|
return result;
|
|
1095
1133
|
};
|
|
1096
1134
|
exports.quantizeWithPalette = quantizeWithPalette;
|
|
@@ -1104,11 +1142,11 @@ const quantizeToIndices = async (image, k, options) => {
|
|
|
1104
1142
|
const report = reporter(o);
|
|
1105
1143
|
const clustering = await clusterInSlices(wasm, image, o, report, 0.9);
|
|
1106
1144
|
checkAborted(o.signal);
|
|
1107
|
-
const indexed = unpackIndices(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.width, o.transparent)), original, (0, _shared.colorSpaceName)(o.colorSpace));
|
|
1145
|
+
const indexed = unpackIndices(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.ditherMethod, o.ditherSteps, o.width, o.transparent)), original, (0, _shared.colorSpaceName)(o.colorSpace));
|
|
1108
1146
|
report(1);
|
|
1109
1147
|
return indexed;
|
|
1110
1148
|
}
|
|
1111
|
-
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.width, o.transparent), o.precision), original, (0, _shared.colorSpaceName)(o.colorSpace));
|
|
1149
|
+
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), original, (0, _shared.colorSpaceName)(o.colorSpace));
|
|
1112
1150
|
};
|
|
1113
1151
|
exports.quantizeToIndices = quantizeToIndices;
|
|
1114
1152
|
const quantizeToIndicesFromClustering = async (image, clustering, k, options) => {
|
|
@@ -1124,7 +1162,7 @@ const quantizeToIndicesFromClustering = async (image, clustering, k, options) =>
|
|
|
1124
1162
|
|
|
1125
1163
|
// The cutoff the clustering was made with is the default, so clustering and mapping agree unless a cutoff is named.
|
|
1126
1164
|
const cutoff = o.transparentGiven ? o.transparent : header.transparent;
|
|
1127
|
-
const result = unpackIndices(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.width, cutoff)), original, (0, _shared.colorSpaceName)(header.colorSpace));
|
|
1165
|
+
const result = unpackIndices(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.ditherMethod, o.ditherSteps, o.width, cutoff)), original, (0, _shared.colorSpaceName)(header.colorSpace));
|
|
1128
1166
|
return result;
|
|
1129
1167
|
};
|
|
1130
1168
|
exports.quantizeToIndicesFromClustering = quantizeToIndicesFromClustering;
|
|
@@ -1139,7 +1177,7 @@ const quantizeToIndicesWithPalette = async (image, palette, options) => {
|
|
|
1139
1177
|
palette = held(palette);
|
|
1140
1178
|
const wasm = Module || (await acquire());
|
|
1141
1179
|
checkAborted(o.signal);
|
|
1142
|
-
const result = unpackIndices(call(wasm, [image.data, palette], (imagePointer, palettePointer) => wasm._quantize_indices_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, space, o.dither, o.width, o.transparent)), original, paletteSpace);
|
|
1180
|
+
const result = unpackIndices(call(wasm, [image.data, palette], (imagePointer, palettePointer) => wasm._quantize_indices_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, space, o.dither, o.ditherMethod, o.ditherSteps, o.width, o.transparent)), original, paletteSpace);
|
|
1143
1181
|
return result;
|
|
1144
1182
|
};
|
|
1145
1183
|
exports.quantizeToIndicesWithPalette = quantizeToIndicesWithPalette;
|
package/dist/index.d.cts
CHANGED
|
@@ -33,6 +33,24 @@ export interface CodedError extends Error {
|
|
|
33
33
|
/** Every `ErrorCode`, in a frozen array: for code that forwards or lists them (a wrapper package, an IPC bridge). */
|
|
34
34
|
export const errorCodes: readonly ErrorCode[];
|
|
35
35
|
|
|
36
|
+
/** Every colour space, as a frozen list: narrow a string with `colorSpaces.find((c) => c === value)`. */
|
|
37
|
+
export const colorSpaces: readonly ColorSpace[];
|
|
38
|
+
|
|
39
|
+
/** Every image format, as a frozen list. A minor release may add one, as it may add an error code. */
|
|
40
|
+
export const imageFormats: readonly ImageFormat[];
|
|
41
|
+
|
|
42
|
+
/** Every alpha mode, as a frozen list. */
|
|
43
|
+
export const alphaModes: readonly AlphaMode[];
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Every dither method, as a frozen list: for building a choice from them, or narrowing a string from a form with
|
|
47
|
+
* `ditherMethods.find((m) => m === value)`.
|
|
48
|
+
*/
|
|
49
|
+
export const ditherMethods: readonly DitherMethod[];
|
|
50
|
+
|
|
51
|
+
/** Every step count ordered dithering takes, as a frozen list: narrow a number with `ditherSteps.find((s) => s === value)`. */
|
|
52
|
+
export const ditherSteps: readonly DitherSteps[];
|
|
53
|
+
|
|
36
54
|
/**
|
|
37
55
|
* Whether `value` is one of this library's errors: something with a string `message` and a `code` from `errorCodes`.
|
|
38
56
|
* Unlike checking `code` alone, it is false for Node's own errors (`'ENOENT'`) and for an abort (the signal's reason).
|
|
@@ -131,7 +149,7 @@ export type ColorSpace = 'rgb' | 'oklab';
|
|
|
131
149
|
/** How pixel opacity affects clustering. */
|
|
132
150
|
export type AlphaMode = 'weight' | 'ignore';
|
|
133
151
|
|
|
134
|
-
/** Cancelling a call. Every function that takes an image has it
|
|
152
|
+
/** Cancelling a call. Every function that takes an image has it. */
|
|
135
153
|
export interface CancelOptions {
|
|
136
154
|
/**
|
|
137
155
|
* Cancels the call. A call given a `signal` clusters (where it clusters) in slices of about 10 ms and checks the
|
|
@@ -146,8 +164,8 @@ export interface CancelOptions {
|
|
|
146
164
|
signal?: AbortSignal | undefined;
|
|
147
165
|
}
|
|
148
166
|
|
|
149
|
-
/** Cancelling a long call and watching
|
|
150
|
-
export interface
|
|
167
|
+
/** Cancelling a long call and watching its progress, for the functions that cluster. Both are optional and can be used apart. */
|
|
168
|
+
export interface ProgressOptions extends CancelOptions {
|
|
151
169
|
/**
|
|
152
170
|
* Called with the fraction done, from 0 to 1, as the call runs: a few times per slice at most, never going
|
|
153
171
|
* backwards, ending with 1. It is approximate and not linear in time. Giving it also makes the call run in
|
|
@@ -157,10 +175,10 @@ export interface RunOptions extends CancelOptions {
|
|
|
157
175
|
}
|
|
158
176
|
|
|
159
177
|
/** Options for building the colour hierarchy: `getClustering`, `getPalette`, and the clustering half of `quantize` and `quantizeToIndices`. */
|
|
160
|
-
export interface ClusteringOptions extends
|
|
178
|
+
export interface ClusteringOptions extends ProgressOptions {
|
|
161
179
|
/**
|
|
162
180
|
* `'rgb'` (default) minimises squared sRGB error, which is what MSE/PSNR measure.
|
|
163
|
-
* `'oklab'` clusters in OKLab. Neither is better everywhere: see "Choosing a colour space" in
|
|
181
|
+
* `'oklab'` clusters in OKLab. Neither is better everywhere: see "Choosing a colour space" in docs/api.md.
|
|
164
182
|
*/
|
|
165
183
|
colorSpace?: ColorSpace | undefined;
|
|
166
184
|
/**
|
|
@@ -187,6 +205,31 @@ export interface ClusteringOptions extends RunOptions {
|
|
|
187
205
|
transparent?: boolean | number | undefined;
|
|
188
206
|
}
|
|
189
207
|
|
|
208
|
+
/**
|
|
209
|
+
* How dithering spreads the difference between a pixel and its palette colour.
|
|
210
|
+
* - `'floyd-steinberg'`: error diffusion, the most accurate for still images (what `dither: true` means).
|
|
211
|
+
* - `'atkinson'`: error diffusion that passes on only 3/4 of the error, for a lighter, higher-contrast look.
|
|
212
|
+
* - `'ordered'`: a fixed 8x8 pattern, so a pixel's colour depends only on its own colour and position: still areas of
|
|
213
|
+
* an animation stay still from frame to frame.
|
|
214
|
+
*/
|
|
215
|
+
export type DitherMethod = 'floyd-steinberg' | 'atkinson' | 'ordered';
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The step counts ordered dithering takes: the counts that divide the 64 cells of its 8x8 pattern evenly. Narrow a
|
|
219
|
+
* number to it with the exported list: `ditherSteps.find((s) => s === value)`.
|
|
220
|
+
*/
|
|
221
|
+
export type DitherSteps = 2 | 4 | 8 | 16 | 32 | 64;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* The full form of the `dither` option. `strength` (0 to 1, default 1) scales how much of the error is spread; 0 is no
|
|
225
|
+
* dithering. `steps` is for `'ordered'` only: how many pattern cells each colour's mix is spread over, so two colours
|
|
226
|
+
* blend in `steps + 1` shades. The default is 64 for a palette of two colours and 16 otherwise; each doubling about
|
|
227
|
+
* doubles the time.
|
|
228
|
+
*/
|
|
229
|
+
export type DitherOptions =
|
|
230
|
+
| { method?: 'floyd-steinberg' | 'atkinson' | undefined; strength?: number | undefined; steps?: undefined }
|
|
231
|
+
| { method: 'ordered'; strength?: number | undefined; steps?: DitherSteps | undefined };
|
|
232
|
+
|
|
190
233
|
/** Options for mapping pixels to a palette: `quantizeFromClustering`, `quantizeWithPalette`, and the mapping half of `quantize` and `quantizeToIndices`. They take `signal` but no `onProgress`: mapping takes milliseconds. */
|
|
191
234
|
export interface MappingOptions extends CancelOptions {
|
|
192
235
|
/**
|
|
@@ -197,10 +240,12 @@ export interface MappingOptions extends CancelOptions {
|
|
|
197
240
|
*/
|
|
198
241
|
colorSpace?: ColorSpace | undefined;
|
|
199
242
|
/**
|
|
200
|
-
*
|
|
201
|
-
*
|
|
243
|
+
* Dithering: `false` (default); `true` or a strength from 0 to 1 for Floyd-Steinberg error diffusion; a method name
|
|
244
|
+
* (`'floyd-steinberg'`, `'atkinson'`, `'ordered'`); or a {@link DitherOptions} object, which the other forms are
|
|
245
|
+
* shorthands for. A strength of 0 is no dithering. Dithering needs the image width (not at strength 0): taken from
|
|
246
|
+
* `ImageData` or `RawImage.width`, or given as `width`. See "Choosing a dither method" in docs/api.md.
|
|
202
247
|
*/
|
|
203
|
-
dither?: boolean | number | undefined;
|
|
248
|
+
dither?: boolean | number | DitherMethod | DitherOptions | undefined;
|
|
204
249
|
/**
|
|
205
250
|
* Image width in pixels, for dithering a `Uint8Array` or a `RawImage` without `width`. An image that has its own
|
|
206
251
|
* `width` (`ImageData`, or a `RawImage` with one) must agree with it: a different value is an `invalid-argument` error.
|
package/dist/index.d.ts
CHANGED
|
@@ -33,6 +33,24 @@ export interface CodedError extends Error {
|
|
|
33
33
|
/** Every `ErrorCode`, in a frozen array: for code that forwards or lists them (a wrapper package, an IPC bridge). */
|
|
34
34
|
export const errorCodes: readonly ErrorCode[];
|
|
35
35
|
|
|
36
|
+
/** Every colour space, as a frozen list: narrow a string with `colorSpaces.find((c) => c === value)`. */
|
|
37
|
+
export const colorSpaces: readonly ColorSpace[];
|
|
38
|
+
|
|
39
|
+
/** Every image format, as a frozen list. A minor release may add one, as it may add an error code. */
|
|
40
|
+
export const imageFormats: readonly ImageFormat[];
|
|
41
|
+
|
|
42
|
+
/** Every alpha mode, as a frozen list. */
|
|
43
|
+
export const alphaModes: readonly AlphaMode[];
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Every dither method, as a frozen list: for building a choice from them, or narrowing a string from a form with
|
|
47
|
+
* `ditherMethods.find((m) => m === value)`.
|
|
48
|
+
*/
|
|
49
|
+
export const ditherMethods: readonly DitherMethod[];
|
|
50
|
+
|
|
51
|
+
/** Every step count ordered dithering takes, as a frozen list: narrow a number with `ditherSteps.find((s) => s === value)`. */
|
|
52
|
+
export const ditherSteps: readonly DitherSteps[];
|
|
53
|
+
|
|
36
54
|
/**
|
|
37
55
|
* Whether `value` is one of this library's errors: something with a string `message` and a `code` from `errorCodes`.
|
|
38
56
|
* Unlike checking `code` alone, it is false for Node's own errors (`'ENOENT'`) and for an abort (the signal's reason).
|
|
@@ -131,7 +149,7 @@ export type ColorSpace = 'rgb' | 'oklab';
|
|
|
131
149
|
/** How pixel opacity affects clustering. */
|
|
132
150
|
export type AlphaMode = 'weight' | 'ignore';
|
|
133
151
|
|
|
134
|
-
/** Cancelling a call. Every function that takes an image has it
|
|
152
|
+
/** Cancelling a call. Every function that takes an image has it. */
|
|
135
153
|
export interface CancelOptions {
|
|
136
154
|
/**
|
|
137
155
|
* Cancels the call. A call given a `signal` clusters (where it clusters) in slices of about 10 ms and checks the
|
|
@@ -146,8 +164,8 @@ export interface CancelOptions {
|
|
|
146
164
|
signal?: AbortSignal | undefined;
|
|
147
165
|
}
|
|
148
166
|
|
|
149
|
-
/** Cancelling a long call and watching
|
|
150
|
-
export interface
|
|
167
|
+
/** Cancelling a long call and watching its progress, for the functions that cluster. Both are optional and can be used apart. */
|
|
168
|
+
export interface ProgressOptions extends CancelOptions {
|
|
151
169
|
/**
|
|
152
170
|
* Called with the fraction done, from 0 to 1, as the call runs: a few times per slice at most, never going
|
|
153
171
|
* backwards, ending with 1. It is approximate and not linear in time. Giving it also makes the call run in
|
|
@@ -157,10 +175,10 @@ export interface RunOptions extends CancelOptions {
|
|
|
157
175
|
}
|
|
158
176
|
|
|
159
177
|
/** Options for building the colour hierarchy: `getClustering`, `getPalette`, and the clustering half of `quantize` and `quantizeToIndices`. */
|
|
160
|
-
export interface ClusteringOptions extends
|
|
178
|
+
export interface ClusteringOptions extends ProgressOptions {
|
|
161
179
|
/**
|
|
162
180
|
* `'rgb'` (default) minimises squared sRGB error, which is what MSE/PSNR measure.
|
|
163
|
-
* `'oklab'` clusters in OKLab. Neither is better everywhere: see "Choosing a colour space" in
|
|
181
|
+
* `'oklab'` clusters in OKLab. Neither is better everywhere: see "Choosing a colour space" in docs/api.md.
|
|
164
182
|
*/
|
|
165
183
|
colorSpace?: ColorSpace | undefined;
|
|
166
184
|
/**
|
|
@@ -187,6 +205,31 @@ export interface ClusteringOptions extends RunOptions {
|
|
|
187
205
|
transparent?: boolean | number | undefined;
|
|
188
206
|
}
|
|
189
207
|
|
|
208
|
+
/**
|
|
209
|
+
* How dithering spreads the difference between a pixel and its palette colour.
|
|
210
|
+
* - `'floyd-steinberg'`: error diffusion, the most accurate for still images (what `dither: true` means).
|
|
211
|
+
* - `'atkinson'`: error diffusion that passes on only 3/4 of the error, for a lighter, higher-contrast look.
|
|
212
|
+
* - `'ordered'`: a fixed 8x8 pattern, so a pixel's colour depends only on its own colour and position: still areas of
|
|
213
|
+
* an animation stay still from frame to frame.
|
|
214
|
+
*/
|
|
215
|
+
export type DitherMethod = 'floyd-steinberg' | 'atkinson' | 'ordered';
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The step counts ordered dithering takes: the counts that divide the 64 cells of its 8x8 pattern evenly. Narrow a
|
|
219
|
+
* number to it with the exported list: `ditherSteps.find((s) => s === value)`.
|
|
220
|
+
*/
|
|
221
|
+
export type DitherSteps = 2 | 4 | 8 | 16 | 32 | 64;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* The full form of the `dither` option. `strength` (0 to 1, default 1) scales how much of the error is spread; 0 is no
|
|
225
|
+
* dithering. `steps` is for `'ordered'` only: how many pattern cells each colour's mix is spread over, so two colours
|
|
226
|
+
* blend in `steps + 1` shades. The default is 64 for a palette of two colours and 16 otherwise; each doubling about
|
|
227
|
+
* doubles the time.
|
|
228
|
+
*/
|
|
229
|
+
export type DitherOptions =
|
|
230
|
+
| { method?: 'floyd-steinberg' | 'atkinson' | undefined; strength?: number | undefined; steps?: undefined }
|
|
231
|
+
| { method: 'ordered'; strength?: number | undefined; steps?: DitherSteps | undefined };
|
|
232
|
+
|
|
190
233
|
/** Options for mapping pixels to a palette: `quantizeFromClustering`, `quantizeWithPalette`, and the mapping half of `quantize` and `quantizeToIndices`. They take `signal` but no `onProgress`: mapping takes milliseconds. */
|
|
191
234
|
export interface MappingOptions extends CancelOptions {
|
|
192
235
|
/**
|
|
@@ -197,10 +240,12 @@ export interface MappingOptions extends CancelOptions {
|
|
|
197
240
|
*/
|
|
198
241
|
colorSpace?: ColorSpace | undefined;
|
|
199
242
|
/**
|
|
200
|
-
*
|
|
201
|
-
*
|
|
243
|
+
* Dithering: `false` (default); `true` or a strength from 0 to 1 for Floyd-Steinberg error diffusion; a method name
|
|
244
|
+
* (`'floyd-steinberg'`, `'atkinson'`, `'ordered'`); or a {@link DitherOptions} object, which the other forms are
|
|
245
|
+
* shorthands for. A strength of 0 is no dithering. Dithering needs the image width (not at strength 0): taken from
|
|
246
|
+
* `ImageData` or `RawImage.width`, or given as `width`. See "Choosing a dither method" in docs/api.md.
|
|
202
247
|
*/
|
|
203
|
-
dither?: boolean | number | undefined;
|
|
248
|
+
dither?: boolean | number | DitherMethod | DitherOptions | undefined;
|
|
204
249
|
/**
|
|
205
250
|
* Image width in pixels, for dithering a `Uint8Array` or a `RawImage` without `width`. An image that has its own
|
|
206
251
|
* `width` (`ImageData`, or a `RawImage` with one) must agree with it: a different value is an `invalid-argument` error.
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import createWasmModule from './clustering.js';
|
|
2
|
-
import { version, fail, colorSpaces, colorSpaceName, isUint8Array, isUint8ClampedArray, isArrayBufferLike, isOptionsObject, snapshot, isPalette, isClustering, markPalette, markClustering, imageHint, latestVersion, readHeader, checkPalette, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, sizeText, tooLarge } from "./shared.mjs";
|
|
2
|
+
import { parseDither, version, fail, colorSpaceCodes, colorSpaces, colorSpaceName, isUint8Array, isUint8ClampedArray, isArrayBufferLike, isOptionsObject, snapshot, isPalette, isClustering, markPalette, markClustering, imageHint, latestVersion, readHeader, checkPalette, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, sizeText, tooLarge } from "./shared.mjs";
|
|
3
3
|
|
|
4
4
|
// The library's version, the stored-data format version, and the Palette and Clustering types (see shared.js).
|
|
5
|
-
export { version, Palette, Clustering, errorCodes, isCodedError } from "./shared.mjs";
|
|
5
|
+
export { version, Palette, Clustering, errorCodes, isCodedError, ditherMethods, ditherSteps, colorSpaces, imageFormats, alphaModes } from "./shared.mjs";
|
|
6
6
|
// The version of stored data (clusterings) this build writes. It reads this one and format 2 (see readHeader). Unlike
|
|
7
7
|
// `version`, it changes only when the stored format does, so a 3.x release that keeps the format keeps this number.
|
|
8
8
|
export const formatVersion = latestVersion;
|
|
@@ -17,7 +17,7 @@ const codes = {
|
|
|
17
17
|
'rgba': 0,
|
|
18
18
|
'rgb': 1
|
|
19
19
|
};
|
|
20
|
-
const
|
|
20
|
+
const alphaModeCodes = {
|
|
21
21
|
'ignore': 0,
|
|
22
22
|
'weight': 1
|
|
23
23
|
};
|
|
@@ -110,7 +110,7 @@ const wasmLoadFailed = (cause, given) => {
|
|
|
110
110
|
let advice;
|
|
111
111
|
if (/disallowed by embedder|code generation disallowed/i.test(text) && !isNodeLike && !isBrowserLike()) {
|
|
112
112
|
// Edge runtimes (Cloudflare Workers, Vercel's edge runtime) compile WebAssembly only at build time, from an import.
|
|
113
|
-
advice = "This runtime doesn't compile WebAssembly while it runs, from bytes or from a URL (edge runtimes such as Cloudflare Workers and Vercel's): import clustering.wasm as a module, which the bundler compiles when it builds (in Wrangler a plain import of 'agglomerative-clustering/clustering.wasm'; in Next.js a relative path with ?module), and pass it to init({ wasm }) before the first call, as the
|
|
113
|
+
advice = "This runtime doesn't compile WebAssembly while it runs, from bytes or from a URL (edge runtimes such as Cloudflare Workers and Vercel's): import clustering.wasm as a module, which the bundler compiles when it builds (in Wrangler a plain import of 'agglomerative-clustering/clustering.wasm'; in Next.js a relative path with ?module), and pass it to init({ wasm }) before the first call, as the docs show for Cloudflare Workers and Next.js (https://github.com/RohanFredriksson/agglomerative-clustering/blob/master/docs/environments.md).";
|
|
114
114
|
} else if (/(memory|allocate)/i.test(text) && /(could not|cannot|can't|failed to|out of) (allocate|memory|reserve)/i.test(text)) {
|
|
115
115
|
// A 64-bit engine reserves the module's whole address range up front (about 10 GB of virtual memory per instance),
|
|
116
116
|
// though it only uses what it needs; an address-space limit refuses that reservation.
|
|
@@ -156,7 +156,7 @@ const wasmLoadFailed = (cause, given) => {
|
|
|
156
156
|
if (/Invalid URL/i.test(reason)) {
|
|
157
157
|
reason = "the package can't tell where clustering.wasm is in this runtime";
|
|
158
158
|
}
|
|
159
|
-
advice = "Give the module to init({ wasm }) before the first call: in an edge runtime (Cloudflare Workers, Vercel's), import clustering.wasm as a compiled module (the
|
|
159
|
+
advice = "Give the module to init({ wasm }) before the first call: in an edge runtime (Cloudflare Workers, Vercel's), import clustering.wasm as a compiled module (the docs show how for Cloudflare Workers and Next.js: https://github.com/RohanFredriksson/agglomerative-clustering/blob/master/docs/environments.md); elsewhere its bytes or URL.";
|
|
160
160
|
} else {
|
|
161
161
|
advice = "Check that clustering.wasm is served next to the script (not a 404, and with the application/wasm type) and allowed by the page's Content-Security-Policy, or say where it is with init({ wasm }).";
|
|
162
162
|
}
|
|
@@ -590,12 +590,15 @@ const check = k => {
|
|
|
590
590
|
// clustering, the default is the space the clustering was made in (code -1)
|
|
591
591
|
// alpha: 'weight' (default) counts RGBA pixels in proportion to their opacity; 'ignore' counts every pixel
|
|
592
592
|
// precision: bits per channel, 1-8 (default 8, exact); lower values group similar colours before clustering
|
|
593
|
-
// dither: false (default), true
|
|
593
|
+
// dither: false (default), true or a strength in [0, 1] (Floyd-Steinberg), a method name, or { method, strength, steps } (see parseDither)
|
|
594
594
|
// width: image width in pixels, needed to dither a Uint8Array or a RawImage without `width`
|
|
595
595
|
// signal, onProgress: an AbortSignal and a progress callback, which make the clustering run in slices (see clusterInSlices)
|
|
596
596
|
// 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);
|
|
597
597
|
// indexed output also gives them their own entry, which counts towards k
|
|
598
598
|
|
|
599
|
+
// The settings of dither, which a caller may write as options of their own; naming one of these gets a pointer to the
|
|
600
|
+
// object form.
|
|
601
|
+
const ditherSettings = ['ditherMethod', 'ditherStrength', 'ditherSteps', 'method', 'strength', 'steps', 'mix'];
|
|
599
602
|
const parseOptions = (options, allowed, image) => {
|
|
600
603
|
if (options === undefined) {
|
|
601
604
|
options = {};
|
|
@@ -606,25 +609,27 @@ const parseOptions = (options, allowed, image) => {
|
|
|
606
609
|
options = snapshot(options);
|
|
607
610
|
for (const key of Object.keys(options)) {
|
|
608
611
|
if (!allowed.includes(key)) {
|
|
609
|
-
|
|
612
|
+
const hint = allowed.includes('dither') && ditherSettings.includes(key) ? ' Dithering settings go inside the dither option: dither: { method, strength, steps }.' : '';
|
|
613
|
+
throw fail('invalid-argument', `Invalid option '${key}': expected one of ${allowed.join(', ')}.${hint}`, `options.${key}`);
|
|
610
614
|
}
|
|
611
615
|
}
|
|
612
616
|
const colorSpace = options.colorSpace === undefined ? 'rgb' : options.colorSpace;
|
|
613
|
-
if (!
|
|
617
|
+
if (!colorSpaces.includes(colorSpace)) {
|
|
614
618
|
throw fail('invalid-argument', "Invalid colorSpace: must be 'rgb' or 'oklab'.", 'options.colorSpace');
|
|
615
619
|
}
|
|
616
620
|
const alpha = options.alpha === undefined ? 'weight' : options.alpha;
|
|
617
|
-
if (!Object.keys(
|
|
621
|
+
if (!Object.keys(alphaModeCodes).includes(alpha)) {
|
|
618
622
|
throw fail('invalid-argument', "Invalid alpha: must be 'weight' or 'ignore'.", 'options.alpha');
|
|
619
623
|
}
|
|
620
624
|
const precision = options.precision === undefined ? 8 : options.precision;
|
|
621
625
|
if (typeof precision !== 'number' || !Number.isInteger(precision) || precision < 1 || precision > 8) {
|
|
622
626
|
throw fail('invalid-argument', "Invalid precision: must be an integer from 1 to 8.", 'options.precision');
|
|
623
627
|
}
|
|
624
|
-
const
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
+
const {
|
|
629
|
+
strength: dither,
|
|
630
|
+
method: ditherMethod,
|
|
631
|
+
steps: ditherSteps
|
|
632
|
+
} = parseDither(options.dither);
|
|
628
633
|
|
|
629
634
|
// The image's own width and options.width are two sources for one fact: they may repeat each other, but not disagree
|
|
630
635
|
// (a wrong row length would dither without an error, as long as it divides the pixel count).
|
|
@@ -665,11 +670,13 @@ const parseOptions = (options, allowed, image) => {
|
|
|
665
670
|
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');
|
|
666
671
|
}
|
|
667
672
|
return {
|
|
668
|
-
colorSpace:
|
|
673
|
+
colorSpace: colorSpaceCodes[colorSpace],
|
|
669
674
|
colorSpaceGiven: options.colorSpace !== undefined,
|
|
670
|
-
alpha:
|
|
675
|
+
alpha: alphaModeCodes[alpha],
|
|
671
676
|
precision,
|
|
672
677
|
dither,
|
|
678
|
+
ditherMethod,
|
|
679
|
+
ditherSteps,
|
|
673
680
|
width: dither > 0 ? width : 0,
|
|
674
681
|
transparent,
|
|
675
682
|
transparentGiven: options.transparent !== undefined,
|
|
@@ -704,7 +711,7 @@ const checkNotEmpty = (palette, image, cutoff) => {
|
|
|
704
711
|
};
|
|
705
712
|
|
|
706
713
|
// The space to match pixels to a palette in: the one the caller names, else the one a Palette was made in, else RGB.
|
|
707
|
-
const matchingSpace = (o, palette) => o.colorSpaceGiven ? o.colorSpace :
|
|
714
|
+
const matchingSpace = (o, palette) => o.colorSpaceGiven ? o.colorSpace : colorSpaceCodes[isPalette(palette) ? palette.colorSpace : 'rgb'];
|
|
708
715
|
|
|
709
716
|
// Allocates `length` bytes in the WASM heap, or throws a coded out-of-memory error.
|
|
710
717
|
const allocate = (wasm, length) => {
|
|
@@ -1009,11 +1016,11 @@ export const quantize = async (image, k, options) => {
|
|
|
1009
1016
|
const report = reporter(o);
|
|
1010
1017
|
const clustering = await clusterInSlices(wasm, image, o, report, 0.9);
|
|
1011
1018
|
checkAborted(o.signal);
|
|
1012
|
-
const mapped = wrap(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.width)));
|
|
1019
|
+
const mapped = wrap(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.ditherMethod, o.ditherSteps, o.width)));
|
|
1013
1020
|
report(1);
|
|
1014
1021
|
return mapped;
|
|
1015
1022
|
}
|
|
1016
|
-
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.width, o.transparent), o.precision));
|
|
1023
|
+
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));
|
|
1017
1024
|
};
|
|
1018
1025
|
export const quantizeFromClustering = async (image, clustering, k, options) => {
|
|
1019
1026
|
const wrap = getWrapper(image);
|
|
@@ -1025,7 +1032,7 @@ export const quantizeFromClustering = async (image, clustering, k, options) => {
|
|
|
1025
1032
|
const wasm = Module || (await acquire());
|
|
1026
1033
|
checkAborted(o.signal);
|
|
1027
1034
|
const colorSpace = o.colorSpaceGiven ? o.colorSpace : -1;
|
|
1028
|
-
const result = wrap(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.width)));
|
|
1035
|
+
const result = wrap(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.ditherMethod, o.ditherSteps, o.width)));
|
|
1029
1036
|
return result;
|
|
1030
1037
|
};
|
|
1031
1038
|
export const quantizeWithPalette = async (image, palette, options) => {
|
|
@@ -1038,7 +1045,7 @@ export const quantizeWithPalette = async (image, palette, options) => {
|
|
|
1038
1045
|
palette = held(palette);
|
|
1039
1046
|
const wasm = Module || (await acquire());
|
|
1040
1047
|
checkAborted(o.signal);
|
|
1041
|
-
const result = wrap(call(wasm, [image.data, palette], (imagePointer, palettePointer) => wasm._quantize_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, space, o.dither, o.width)));
|
|
1048
|
+
const result = wrap(call(wasm, [image.data, palette], (imagePointer, palettePointer) => wasm._quantize_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, space, o.dither, o.ditherMethod, o.ditherSteps, o.width)));
|
|
1042
1049
|
return result;
|
|
1043
1050
|
};
|
|
1044
1051
|
export const quantizeToIndices = async (image, k, options) => {
|
|
@@ -1051,11 +1058,11 @@ export const quantizeToIndices = async (image, k, options) => {
|
|
|
1051
1058
|
const report = reporter(o);
|
|
1052
1059
|
const clustering = await clusterInSlices(wasm, image, o, report, 0.9);
|
|
1053
1060
|
checkAborted(o.signal);
|
|
1054
|
-
const indexed = unpackIndices(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.width, o.transparent)), original, colorSpaceName(o.colorSpace));
|
|
1061
|
+
const indexed = unpackIndices(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.ditherMethod, o.ditherSteps, o.width, o.transparent)), original, colorSpaceName(o.colorSpace));
|
|
1055
1062
|
report(1);
|
|
1056
1063
|
return indexed;
|
|
1057
1064
|
}
|
|
1058
|
-
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.width, o.transparent), o.precision), original, colorSpaceName(o.colorSpace));
|
|
1065
|
+
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), original, colorSpaceName(o.colorSpace));
|
|
1059
1066
|
};
|
|
1060
1067
|
export const quantizeToIndicesFromClustering = async (image, clustering, k, options) => {
|
|
1061
1068
|
const original = image;
|
|
@@ -1070,7 +1077,7 @@ export const quantizeToIndicesFromClustering = async (image, clustering, k, opti
|
|
|
1070
1077
|
|
|
1071
1078
|
// The cutoff the clustering was made with is the default, so clustering and mapping agree unless a cutoff is named.
|
|
1072
1079
|
const cutoff = o.transparentGiven ? o.transparent : header.transparent;
|
|
1073
|
-
const result = unpackIndices(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.width, cutoff)), original, colorSpaceName(header.colorSpace));
|
|
1080
|
+
const result = unpackIndices(call(wasm, [image.data, clustering], (imagePointer, clusteringPointer) => wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.ditherMethod, o.ditherSteps, o.width, cutoff)), original, colorSpaceName(header.colorSpace));
|
|
1074
1081
|
return result;
|
|
1075
1082
|
};
|
|
1076
1083
|
export const quantizeToIndicesWithPalette = async (image, palette, options) => {
|
|
@@ -1084,6 +1091,6 @@ export const quantizeToIndicesWithPalette = async (image, palette, options) => {
|
|
|
1084
1091
|
palette = held(palette);
|
|
1085
1092
|
const wasm = Module || (await acquire());
|
|
1086
1093
|
checkAborted(o.signal);
|
|
1087
|
-
const result = unpackIndices(call(wasm, [image.data, palette], (imagePointer, palettePointer) => wasm._quantize_indices_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, space, o.dither, o.width, o.transparent)), original, paletteSpace);
|
|
1094
|
+
const result = unpackIndices(call(wasm, [image.data, palette], (imagePointer, palettePointer) => wasm._quantize_indices_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, space, o.dither, o.ditherMethod, o.ditherSteps, o.width, o.transparent)), original, paletteSpace);
|
|
1088
1095
|
return result;
|
|
1089
1096
|
};
|