agglomerative-clustering 3.0.0 → 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 +1 -1
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +59 -21
- package/dist/index.d.cts +48 -3
- package/dist/index.d.ts +48 -3
- package/dist/index.mjs +29 -22
- 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 +4 -1
- package/dist/worker.d.ts +4 -1
- package/dist/worker.mjs +10 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -101,7 +101,7 @@ The one-shot functions (`getPalette`, `quantize`, …) do every step for you. To
|
|
|
101
101
|
|
|
102
102
|
**`…FromClustering`** takes the colours from a clustering you already hold, and **`…WithPalette`** takes colours you supply; with neither, the image is clustered for you. `getClustering` builds a clustering, `getClusteringInfo` reads one (including its error curve, for choosing a palette size), and `init` loads the WebAssembly module (optional). Most code only needs the first column.
|
|
103
103
|
|
|
104
|
-
Palettes come in **split order**: the palette for `k + 1` is the one for `k` with one colour split in two, so entries keep their place as `k` grows (they are not sorted by share). The options are `precision` (6 is about 3× faster at practically the same quality), `colorSpace` (`'rgb'` or `'oklab'`), `alpha`, `transparent`, `dither
|
|
104
|
+
Palettes come in **split order**: the palette for `k + 1` is the one for `k` with one colour split in two, so entries keep their place as `k` grows (they are not sorted by share). The options are `precision` (6 is about 3× faster at practically the same quality), `colorSpace` (`'rgb'` or `'oklab'`), `alpha`, `transparent`, `dither` (Floyd–Steinberg, Atkinson, or ordered for animation, which keeps still areas still), `width`, `signal` and `onProgress`: see the [API reference](https://github.com/RohanFredriksson/agglomerative-clustering/blob/master/docs/api.md).
|
|
105
105
|
|
|
106
106
|
## Use cases
|
|
107
107
|
|
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
|
};
|
|
@@ -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).
|
|
@@ -187,6 +205,31 @@ export interface ClusteringOptions extends ProgressOptions {
|
|
|
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).
|
|
@@ -187,6 +205,31 @@ export interface ClusteringOptions extends ProgressOptions {
|
|
|
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
|
};
|
|
@@ -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
|
};
|
package/dist/shared.cjs
CHANGED
|
@@ -6,18 +6,23 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.Clustering = Clustering;
|
|
7
7
|
exports.MARK = void 0;
|
|
8
8
|
exports.Palette = Palette;
|
|
9
|
-
exports.version = exports.tooLarge = exports.snapshot = exports.sizeText = exports.revive = exports.readHeader = exports.maxBytes = exports.markPalette = exports.markClustering = exports.mark = exports.latestVersion = exports.isUint8ClampedArray = exports.isUint8Array = exports.isPalette = exports.isOptionsObject = exports.isImageData = exports.isDetached = exports.isCodedError = exports.isClustering = exports.isArrayBufferLike = exports.imageHint = exports.imageDataLike = exports.formatVersion = exports.fail = exports.errorCodes = exports.colorSpaces = exports.colorSpaceName = exports.checkPalette = exports.checkImageDataPixels = void 0;
|
|
9
|
+
exports.version = exports.tooLarge = exports.snapshot = exports.sizeText = exports.revive = exports.readHeader = exports.parseDither = exports.maxBytes = exports.markPalette = exports.markClustering = exports.mark = exports.latestVersion = exports.isUint8ClampedArray = exports.isUint8Array = exports.isPalette = exports.isOptionsObject = exports.isImageData = exports.isDetached = exports.isCodedError = exports.isClustering = exports.isArrayBufferLike = exports.imageHint = exports.imageFormats = exports.imageDataLike = exports.formatVersion = exports.fail = exports.errorCodes = exports.ditherSteps = exports.ditherMethods = exports.colorSpaces = exports.colorSpaceName = exports.colorSpaceCodes = exports.checkPalette = exports.checkImageDataPixels = exports.alphaModes = void 0;
|
|
10
10
|
// Shared by both entries (index.js and worker.js) and the worker thread: errors, option snapshots, reading a clustering's
|
|
11
11
|
// header, the Palette and Clustering types, and the markers that carry those types across the worker boundary. Imported as
|
|
12
12
|
// './shared.js'; the build rewrites that to './shared.mjs' or './shared.cjs' (scripts/babel-import-extensions.cjs).
|
|
13
13
|
|
|
14
14
|
// The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
|
|
15
15
|
// is built from, src/version.hpp) in step with it. Do not edit it by hand.
|
|
16
|
-
const version = exports.version = '3.
|
|
17
|
-
const
|
|
16
|
+
const version = exports.version = '3.1.0';
|
|
17
|
+
const colorSpaceCodes = exports.colorSpaceCodes = {
|
|
18
18
|
'rgb': 0,
|
|
19
19
|
'oklab': 1
|
|
20
20
|
};
|
|
21
|
+
// Every option whose value is one of a fixed set of names has the set exported as a frozen list (like errorCodes,
|
|
22
|
+
// ditherMethods and ditherSteps), for building a choice from it or checking a value against it.
|
|
23
|
+
const colorSpaces = exports.colorSpaces = Object.freeze(Object.keys(colorSpaceCodes));
|
|
24
|
+
const imageFormats = exports.imageFormats = Object.freeze(['rgb', 'rgba']);
|
|
25
|
+
const alphaModes = exports.alphaModes = Object.freeze(['weight', 'ignore']);
|
|
21
26
|
const colorSpaceName = code => code === 1 ? 'oklab' : 'rgb';
|
|
22
27
|
|
|
23
28
|
// Every error this library throws is an Error with a `code` saying what went wrong, so callers needn't match on message
|
|
@@ -167,6 +172,93 @@ const snapshot = options => {
|
|
|
167
172
|
return copy;
|
|
168
173
|
};
|
|
169
174
|
|
|
175
|
+
// ----- Dithering ------------------------------------------------------------------------------------------------------
|
|
176
|
+
// The dither methods and their codes in C, and the step counts ordered dithering takes (the counts that divide the 64
|
|
177
|
+
// cells of its 8x8 matrix evenly). Steps 0 tells C to choose: 64 for a palette of two colours, else 16.
|
|
178
|
+
exports.snapshot = snapshot;
|
|
179
|
+
const ditherMethodCodes = {
|
|
180
|
+
'floyd-steinberg': 0,
|
|
181
|
+
'atkinson': 1,
|
|
182
|
+
'ordered': 2
|
|
183
|
+
};
|
|
184
|
+
// The method names and step counts, exported (like errorCodes) for code that builds options from a form.
|
|
185
|
+
const ditherMethods = exports.ditherMethods = Object.freeze(Object.keys(ditherMethodCodes));
|
|
186
|
+
const ditherSteps = exports.ditherSteps = Object.freeze([2, 4, 8, 16, 32, 64]);
|
|
187
|
+
|
|
188
|
+
// A short description of a value for an error message.
|
|
189
|
+
const describe = value => typeof value === 'string' ? `'${value}'` : typeof value === 'number' || typeof value === 'boolean' || value === null ? String(value) : Array.isArray(value) ? 'an array' : typeof value === 'object' ? `a ${Object.prototype.toString.call(value).slice(8, -1)}` : typeof value;
|
|
190
|
+
|
|
191
|
+
// Reads every form of the dither option: false/undefined (off), true or a strength from 0 to 1 (Floyd-Steinberg, as in
|
|
192
|
+
// 3.0), a method name, or an object { method, strength, steps } that the plain forms are shorthands for.
|
|
193
|
+
// Returns {strength, method, steps}, with strength 0 meaning no dithering and steps 0 meaning the default.
|
|
194
|
+
const parseDither = value => {
|
|
195
|
+
const invalid = (key, message) => fail('invalid-argument', message, key === undefined ? 'options.dither' : `options.dither.${key}`);
|
|
196
|
+
// What a value from a form most likely meant: 'none' for no dithering, or a number given as a string.
|
|
197
|
+
const numeric = v => typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v));
|
|
198
|
+
const asNumber = v => numeric(v) ? ` It is a string: pass a number, for example Number(value).` : '';
|
|
199
|
+
const wrongForm = () => invalid(undefined, `Invalid dither: got ${describe(value)}; it must be a boolean, a strength from 0 to 1, 'floyd-steinberg', 'atkinson', 'ordered', or an object { method, strength, steps }.${typeof value === 'string' && ['none', 'off', 'false'].includes(value.trim().toLowerCase()) ? ' Use false for no dithering.' : asNumber(value)}`);
|
|
200
|
+
const has = key => Object.prototype.hasOwnProperty.call(ditherMethodCodes, key);
|
|
201
|
+
if (value === undefined || value === false) {
|
|
202
|
+
return {
|
|
203
|
+
strength: 0,
|
|
204
|
+
method: 0,
|
|
205
|
+
steps: 0
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
if (value === true) {
|
|
209
|
+
return {
|
|
210
|
+
strength: 1,
|
|
211
|
+
method: 0,
|
|
212
|
+
steps: 0
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
if (typeof value === 'number') {
|
|
216
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) {
|
|
217
|
+
throw wrongForm();
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
strength: value,
|
|
221
|
+
method: 0,
|
|
222
|
+
steps: 0
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
if (typeof value === 'string' && has(value)) {
|
|
226
|
+
return {
|
|
227
|
+
strength: 1,
|
|
228
|
+
method: ditherMethodCodes[value],
|
|
229
|
+
steps: 0
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
if (!isOptionsObject(value)) {
|
|
233
|
+
throw wrongForm();
|
|
234
|
+
}
|
|
235
|
+
const object = snapshot(value);
|
|
236
|
+
for (const key of Object.keys(object)) {
|
|
237
|
+
if (!['method', 'strength', 'steps'].includes(key)) {
|
|
238
|
+
throw invalid(key, `Invalid dither option '${key}': expected method, strength or steps.`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const methodName = object.method === undefined ? 'floyd-steinberg' : object.method;
|
|
242
|
+
if (typeof methodName !== 'string' || !has(methodName)) {
|
|
243
|
+
throw invalid('method', `Invalid dither.method: got ${describe(methodName)}; it must be 'floyd-steinberg', 'atkinson' or 'ordered'.`);
|
|
244
|
+
}
|
|
245
|
+
const strength = object.strength === undefined ? 1 : object.strength;
|
|
246
|
+
if (typeof strength !== 'number' || !Number.isFinite(strength) || strength < 0 || strength > 1) {
|
|
247
|
+
throw invalid('strength', `Invalid dither.strength: got ${describe(strength)}; it must be a number from 0 to 1.${asNumber(strength)}`);
|
|
248
|
+
}
|
|
249
|
+
if (object.steps !== undefined && methodName !== 'ordered') {
|
|
250
|
+
throw invalid('steps', `Invalid dither.steps: only ordered dithering takes steps, so write dither: { method: 'ordered', steps }.`);
|
|
251
|
+
}
|
|
252
|
+
if (object.steps !== undefined && !ditherSteps.includes(object.steps)) {
|
|
253
|
+
throw invalid('steps', `Invalid dither.steps: got ${describe(object.steps)}; it must be 2, 4, 8, 16, 32 or 64.${asNumber(object.steps)}`);
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
strength,
|
|
257
|
+
method: ditherMethodCodes[methodName],
|
|
258
|
+
steps: object.steps === undefined ? 0 : object.steps
|
|
259
|
+
};
|
|
260
|
+
};
|
|
261
|
+
|
|
170
262
|
// ----- Palette and Clustering ----------------------------------------------------------------------------------------
|
|
171
263
|
// Palettes and clusterings are byte arrays, and nothing about a Uint8Array says which one it is, or (for a palette) which
|
|
172
264
|
// colour space its colours were made in. So the ones this library returns are ordinary Uint8Arrays carrying two hidden,
|
|
@@ -176,7 +268,7 @@ const snapshot = options => {
|
|
|
176
268
|
// IndexedDB) give plain untagged Uint8Arrays. `Palette` and `Clustering` are how to make one by hand, and `instanceof`
|
|
177
269
|
// recognises them. The tags are found by a symbol registered with Symbol.for, so a copy of the library loaded twice (the
|
|
178
270
|
// ES module and CommonJS builds, a bundler's duplicate, the worker entry's own copy) recognises the other's.
|
|
179
|
-
exports.
|
|
271
|
+
exports.parseDither = parseDither;
|
|
180
272
|
const paletteBrand = Symbol.for('agglomerative-clustering.palette');
|
|
181
273
|
const clusteringBrand = Symbol.for('agglomerative-clustering.clustering');
|
|
182
274
|
const isPalette = value => isUint8Array(value) && value[paletteBrand] === true;
|
|
@@ -223,7 +315,7 @@ const markClustering = bytes => {
|
|
|
223
315
|
// checks would trip over.
|
|
224
316
|
exports.markClustering = markClustering;
|
|
225
317
|
function Palette(source, colorSpace = 'rgb') {
|
|
226
|
-
if (!
|
|
318
|
+
if (!colorSpaces.includes(colorSpace)) {
|
|
227
319
|
throw fail('invalid-argument', "Invalid colorSpace: must be 'rgb' or 'oklab'.", 'colorSpace');
|
|
228
320
|
}
|
|
229
321
|
if (source === undefined) {
|
package/dist/shared.mjs
CHANGED
|
@@ -4,11 +4,16 @@
|
|
|
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.
|
|
8
|
-
export const
|
|
7
|
+
export const version = '3.1.0';
|
|
8
|
+
export const colorSpaceCodes = {
|
|
9
9
|
'rgb': 0,
|
|
10
10
|
'oklab': 1
|
|
11
11
|
};
|
|
12
|
+
// Every option whose value is one of a fixed set of names has the set exported as a frozen list (like errorCodes,
|
|
13
|
+
// ditherMethods and ditherSteps), for building a choice from it or checking a value against it.
|
|
14
|
+
export const colorSpaces = Object.freeze(Object.keys(colorSpaceCodes));
|
|
15
|
+
export const imageFormats = Object.freeze(['rgb', 'rgba']);
|
|
16
|
+
export const alphaModes = Object.freeze(['weight', 'ignore']);
|
|
12
17
|
export const colorSpaceName = code => code === 1 ? 'oklab' : 'rgb';
|
|
13
18
|
|
|
14
19
|
// Every error this library throws is an Error with a `code` saying what went wrong, so callers needn't match on message
|
|
@@ -144,6 +149,92 @@ export const snapshot = options => {
|
|
|
144
149
|
return copy;
|
|
145
150
|
};
|
|
146
151
|
|
|
152
|
+
// ----- Dithering ------------------------------------------------------------------------------------------------------
|
|
153
|
+
// The dither methods and their codes in C, and the step counts ordered dithering takes (the counts that divide the 64
|
|
154
|
+
// cells of its 8x8 matrix evenly). Steps 0 tells C to choose: 64 for a palette of two colours, else 16.
|
|
155
|
+
const ditherMethodCodes = {
|
|
156
|
+
'floyd-steinberg': 0,
|
|
157
|
+
'atkinson': 1,
|
|
158
|
+
'ordered': 2
|
|
159
|
+
};
|
|
160
|
+
// The method names and step counts, exported (like errorCodes) for code that builds options from a form.
|
|
161
|
+
export const ditherMethods = Object.freeze(Object.keys(ditherMethodCodes));
|
|
162
|
+
export const ditherSteps = Object.freeze([2, 4, 8, 16, 32, 64]);
|
|
163
|
+
|
|
164
|
+
// A short description of a value for an error message.
|
|
165
|
+
const describe = value => typeof value === 'string' ? `'${value}'` : typeof value === 'number' || typeof value === 'boolean' || value === null ? String(value) : Array.isArray(value) ? 'an array' : typeof value === 'object' ? `a ${Object.prototype.toString.call(value).slice(8, -1)}` : typeof value;
|
|
166
|
+
|
|
167
|
+
// Reads every form of the dither option: false/undefined (off), true or a strength from 0 to 1 (Floyd-Steinberg, as in
|
|
168
|
+
// 3.0), a method name, or an object { method, strength, steps } that the plain forms are shorthands for.
|
|
169
|
+
// Returns {strength, method, steps}, with strength 0 meaning no dithering and steps 0 meaning the default.
|
|
170
|
+
export const parseDither = value => {
|
|
171
|
+
const invalid = (key, message) => fail('invalid-argument', message, key === undefined ? 'options.dither' : `options.dither.${key}`);
|
|
172
|
+
// What a value from a form most likely meant: 'none' for no dithering, or a number given as a string.
|
|
173
|
+
const numeric = v => typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v));
|
|
174
|
+
const asNumber = v => numeric(v) ? ` It is a string: pass a number, for example Number(value).` : '';
|
|
175
|
+
const wrongForm = () => invalid(undefined, `Invalid dither: got ${describe(value)}; it must be a boolean, a strength from 0 to 1, 'floyd-steinberg', 'atkinson', 'ordered', or an object { method, strength, steps }.${typeof value === 'string' && ['none', 'off', 'false'].includes(value.trim().toLowerCase()) ? ' Use false for no dithering.' : asNumber(value)}`);
|
|
176
|
+
const has = key => Object.prototype.hasOwnProperty.call(ditherMethodCodes, key);
|
|
177
|
+
if (value === undefined || value === false) {
|
|
178
|
+
return {
|
|
179
|
+
strength: 0,
|
|
180
|
+
method: 0,
|
|
181
|
+
steps: 0
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
if (value === true) {
|
|
185
|
+
return {
|
|
186
|
+
strength: 1,
|
|
187
|
+
method: 0,
|
|
188
|
+
steps: 0
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
if (typeof value === 'number') {
|
|
192
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) {
|
|
193
|
+
throw wrongForm();
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
strength: value,
|
|
197
|
+
method: 0,
|
|
198
|
+
steps: 0
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (typeof value === 'string' && has(value)) {
|
|
202
|
+
return {
|
|
203
|
+
strength: 1,
|
|
204
|
+
method: ditherMethodCodes[value],
|
|
205
|
+
steps: 0
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
if (!isOptionsObject(value)) {
|
|
209
|
+
throw wrongForm();
|
|
210
|
+
}
|
|
211
|
+
const object = snapshot(value);
|
|
212
|
+
for (const key of Object.keys(object)) {
|
|
213
|
+
if (!['method', 'strength', 'steps'].includes(key)) {
|
|
214
|
+
throw invalid(key, `Invalid dither option '${key}': expected method, strength or steps.`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const methodName = object.method === undefined ? 'floyd-steinberg' : object.method;
|
|
218
|
+
if (typeof methodName !== 'string' || !has(methodName)) {
|
|
219
|
+
throw invalid('method', `Invalid dither.method: got ${describe(methodName)}; it must be 'floyd-steinberg', 'atkinson' or 'ordered'.`);
|
|
220
|
+
}
|
|
221
|
+
const strength = object.strength === undefined ? 1 : object.strength;
|
|
222
|
+
if (typeof strength !== 'number' || !Number.isFinite(strength) || strength < 0 || strength > 1) {
|
|
223
|
+
throw invalid('strength', `Invalid dither.strength: got ${describe(strength)}; it must be a number from 0 to 1.${asNumber(strength)}`);
|
|
224
|
+
}
|
|
225
|
+
if (object.steps !== undefined && methodName !== 'ordered') {
|
|
226
|
+
throw invalid('steps', `Invalid dither.steps: only ordered dithering takes steps, so write dither: { method: 'ordered', steps }.`);
|
|
227
|
+
}
|
|
228
|
+
if (object.steps !== undefined && !ditherSteps.includes(object.steps)) {
|
|
229
|
+
throw invalid('steps', `Invalid dither.steps: got ${describe(object.steps)}; it must be 2, 4, 8, 16, 32 or 64.${asNumber(object.steps)}`);
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
strength,
|
|
233
|
+
method: ditherMethodCodes[methodName],
|
|
234
|
+
steps: object.steps === undefined ? 0 : object.steps
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
|
|
147
238
|
// ----- Palette and Clustering ----------------------------------------------------------------------------------------
|
|
148
239
|
// Palettes and clusterings are byte arrays, and nothing about a Uint8Array says which one it is, or (for a palette) which
|
|
149
240
|
// colour space its colours were made in. So the ones this library returns are ordinary Uint8Arrays carrying two hidden,
|
|
@@ -195,7 +286,7 @@ export const markClustering = bytes => {
|
|
|
195
286
|
// A function and not a `class`, because it returns a plain Uint8Array and answers `instanceof` itself, which transpiled class
|
|
196
287
|
// checks would trip over.
|
|
197
288
|
export function Palette(source, colorSpace = 'rgb') {
|
|
198
|
-
if (!
|
|
289
|
+
if (!colorSpaces.includes(colorSpace)) {
|
|
199
290
|
throw fail('invalid-argument', "Invalid colorSpace: must be 'rgb' or 'oklab'.", 'colorSpace');
|
|
200
291
|
}
|
|
201
292
|
if (source === undefined) {
|
package/dist/worker-thread.mjs
CHANGED
|
@@ -95,6 +95,20 @@ const optionsIndex = {
|
|
|
95
95
|
// One AbortController per call that can be cancelled, by call id. It exists from the moment the call arrives, so a call
|
|
96
96
|
// still waiting its turn can be cancelled and never starts.
|
|
97
97
|
const controllers = new Map();
|
|
98
|
+
|
|
99
|
+
// A turn of the event loop, so messages already sent to this thread (cancels) are read first. In Node, setImmediate runs
|
|
100
|
+
// after the pending messages. In a browser, a message to itself on a MessageChannel: it is queued behind the messages
|
|
101
|
+
// already waiting, as they are posted messages too, and unlike a zero timeout it isn't stretched to 4 ms when timers
|
|
102
|
+
// nest (as they do along a queue of calls), which made each skipped call cost 4 ms.
|
|
103
|
+
const nextTurn = typeof setImmediate === 'function' ? () => new Promise(resolve => setImmediate(resolve)) : typeof MessageChannel === 'function' ? (() => {
|
|
104
|
+
const channel = new MessageChannel(),
|
|
105
|
+
waiting = [];
|
|
106
|
+
channel.port1.onmessage = () => waiting.shift()();
|
|
107
|
+
return () => new Promise(resolve => {
|
|
108
|
+
waiting.push(resolve);
|
|
109
|
+
channel.port2.postMessage(null);
|
|
110
|
+
});
|
|
111
|
+
})() : () => new Promise(resolve => setTimeout(resolve, 0));
|
|
98
112
|
const respond = async ({
|
|
99
113
|
id,
|
|
100
114
|
method,
|
|
@@ -103,10 +117,15 @@ const respond = async ({
|
|
|
103
117
|
}, post) => {
|
|
104
118
|
const controller = controllers.get(id);
|
|
105
119
|
try {
|
|
120
|
+
// The call before this one may have run in one piece (mapping does), with no turn of the event loop for a cancel
|
|
121
|
+
// of this call to arrive; take one before starting, so a call cancelled while it waited never runs.
|
|
122
|
+
if (controller && !controller.signal.aborted) {
|
|
123
|
+
await nextTurn();
|
|
124
|
+
}
|
|
106
125
|
if (controller && controller.signal.aborted) {
|
|
107
126
|
return;
|
|
108
127
|
}
|
|
109
|
-
if (!Object.
|
|
128
|
+
if (!Object.prototype.hasOwnProperty.call(methods, method)) {
|
|
110
129
|
throw Object.assign(new Error(`Unknown method '${method}'.`), {
|
|
111
130
|
code: 'invalid-argument'
|
|
112
131
|
});
|
package/dist/worker.cjs
CHANGED
|
@@ -17,14 +17,45 @@ Object.defineProperty(exports, "Palette", {
|
|
|
17
17
|
return _shared.Palette;
|
|
18
18
|
}
|
|
19
19
|
});
|
|
20
|
+
Object.defineProperty(exports, "alphaModes", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () {
|
|
23
|
+
return _shared.alphaModes;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(exports, "colorSpaces", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () {
|
|
29
|
+
return _shared.colorSpaces;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
20
32
|
exports.createWorker = void 0;
|
|
33
|
+
Object.defineProperty(exports, "ditherMethods", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
get: function () {
|
|
36
|
+
return _shared.ditherMethods;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(exports, "ditherSteps", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
get: function () {
|
|
42
|
+
return _shared.ditherSteps;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
21
45
|
Object.defineProperty(exports, "errorCodes", {
|
|
22
46
|
enumerable: true,
|
|
23
47
|
get: function () {
|
|
24
48
|
return _shared.errorCodes;
|
|
25
49
|
}
|
|
26
50
|
});
|
|
27
|
-
exports.
|
|
51
|
+
exports.getSwatchesFromClustering = exports.getSwatches = exports.getPaletteFromClustering = exports.getPalette = exports.getClusteringInfo = exports.getClustering = exports.formatVersion = void 0;
|
|
52
|
+
Object.defineProperty(exports, "imageFormats", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () {
|
|
55
|
+
return _shared.imageFormats;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
exports.init = void 0;
|
|
28
59
|
Object.defineProperty(exports, "isCodedError", {
|
|
29
60
|
enumerable: true,
|
|
30
61
|
get: function () {
|
|
@@ -448,6 +479,14 @@ const takeControls = (options, progressAllowed = true) => {
|
|
|
448
479
|
};
|
|
449
480
|
}
|
|
450
481
|
options = (0, _shared.snapshot)(options);
|
|
482
|
+
// The dither setting is an object of its own, which structured cloning would copy without its inherited keys (or not
|
|
483
|
+
// at all, for a Proxy or a function): read it here the way the main entry does, and send a plain copy.
|
|
484
|
+
if (options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
|
|
485
|
+
// One read of each setting (a getter could answer differently twice): the copy is what is checked and sent.
|
|
486
|
+
const copy = (0, _shared.isOptionsObject)(options.dither) ? Object.assign({}, (0, _shared.snapshot)(options.dither)) : options.dither;
|
|
487
|
+
(0, _shared.parseDither)(copy);
|
|
488
|
+
options.dither = copy;
|
|
489
|
+
}
|
|
451
490
|
const {
|
|
452
491
|
signal,
|
|
453
492
|
onProgress
|
package/dist/worker.d.cts
CHANGED
|
@@ -17,6 +17,9 @@ export { Clustering } from './index.cjs';
|
|
|
17
17
|
/** The same list as the main entry's `errorCodes`: the worker entry's errors have the same codes. */
|
|
18
18
|
export { errorCodes } from './index.cjs';
|
|
19
19
|
|
|
20
|
+
/** The same lists as the main entry's: every colour space, image format, alpha mode, dither method and step count. */
|
|
21
|
+
export { colorSpaces, imageFormats, alphaModes, ditherMethods, ditherSteps } from './index.cjs';
|
|
22
|
+
|
|
20
23
|
/** The main entry's `isCodedError`: true for this library's errors, from either entry. */
|
|
21
24
|
export { isCodedError } from './index.cjs';
|
|
22
25
|
|
|
@@ -27,7 +30,7 @@ export const version: string;
|
|
|
27
30
|
export const formatVersion: number;
|
|
28
31
|
|
|
29
32
|
export type {
|
|
30
|
-
ImageFormat, RawImage, ImageLike, PaletteLike, PlatformImageData, ImageResult, WasmModule, ColorSpace, AlphaMode, ClusteringOptions, MappingOptions, QuantizeOptions,
|
|
33
|
+
ImageFormat, RawImage, ImageLike, PaletteLike, PlatformImageData, ImageResult, WasmModule, ColorSpace, AlphaMode, DitherMethod, DitherOptions, DitherSteps, ClusteringOptions, MappingOptions, QuantizeOptions,
|
|
31
34
|
ErrorCode, ErrorArgument, CodedError, InitOptions, ProgressOptions, CancelOptions, IndexOptions, QuantizeIndexOptions, MappingIndexOptions, IndexedImage, ClusteringInfo, Swatches,
|
|
32
35
|
} from './index.cjs';
|
|
33
36
|
|
package/dist/worker.d.ts
CHANGED
|
@@ -17,6 +17,9 @@ export { Clustering } from './index.js';
|
|
|
17
17
|
/** The same list as the main entry's `errorCodes`: the worker entry's errors have the same codes. */
|
|
18
18
|
export { errorCodes } from './index.js';
|
|
19
19
|
|
|
20
|
+
/** The same lists as the main entry's: every colour space, image format, alpha mode, dither method and step count. */
|
|
21
|
+
export { colorSpaces, imageFormats, alphaModes, ditherMethods, ditherSteps } from './index.js';
|
|
22
|
+
|
|
20
23
|
/** The main entry's `isCodedError`: true for this library's errors, from either entry. */
|
|
21
24
|
export { isCodedError } from './index.js';
|
|
22
25
|
|
|
@@ -27,7 +30,7 @@ export const version: string;
|
|
|
27
30
|
export const formatVersion: number;
|
|
28
31
|
|
|
29
32
|
export type {
|
|
30
|
-
ImageFormat, RawImage, ImageLike, PaletteLike, PlatformImageData, ImageResult, WasmModule, ColorSpace, AlphaMode, ClusteringOptions, MappingOptions, QuantizeOptions,
|
|
33
|
+
ImageFormat, RawImage, ImageLike, PaletteLike, PlatformImageData, ImageResult, WasmModule, ColorSpace, AlphaMode, DitherMethod, DitherOptions, DitherSteps, ClusteringOptions, MappingOptions, QuantizeOptions,
|
|
31
34
|
ErrorCode, ErrorArgument, CodedError, InitOptions, ProgressOptions, CancelOptions, IndexOptions, QuantizeIndexOptions, MappingIndexOptions, IndexedImage, ClusteringInfo, Swatches,
|
|
32
35
|
} from './index.js';
|
|
33
36
|
|
package/dist/worker.mjs
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
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, isOptionsObject, snapshot, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge } from "./shared.mjs";
|
|
7
|
+
import { fail, isOptionsObject, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge } 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
|
-
export { version, Palette, Clustering, errorCodes, isCodedError } from "./shared.mjs";
|
|
10
|
+
export { version, Palette, Clustering, errorCodes, isCodedError, ditherMethods, ditherSteps, colorSpaces, imageFormats, alphaModes } from "./shared.mjs";
|
|
11
11
|
export const formatVersion = latestVersion;
|
|
12
12
|
|
|
13
13
|
// Node's worker_threads, where this script runs from a file in Node, Bun or Deno (from npm). A runtime that only imitates Node
|
|
@@ -411,6 +411,14 @@ const takeControls = (options, progressAllowed = true) => {
|
|
|
411
411
|
};
|
|
412
412
|
}
|
|
413
413
|
options = snapshot(options);
|
|
414
|
+
// The dither setting is an object of its own, which structured cloning would copy without its inherited keys (or not
|
|
415
|
+
// at all, for a Proxy or a function): read it here the way the main entry does, and send a plain copy.
|
|
416
|
+
if (options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
|
|
417
|
+
// One read of each setting (a getter could answer differently twice): the copy is what is checked and sent.
|
|
418
|
+
const copy = isOptionsObject(options.dither) ? Object.assign({}, snapshot(options.dither)) : options.dither;
|
|
419
|
+
parseDither(copy);
|
|
420
|
+
options.dither = copy;
|
|
421
|
+
}
|
|
414
422
|
const {
|
|
415
423
|
signal,
|
|
416
424
|
onProgress
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agglomerative-clustering",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.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",
|