agglomerative-clustering 2.0.0 → 3.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +455 -108
- package/dist/clustering.js +2 -14
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +995 -150
- package/dist/index.d.cts +446 -0
- package/dist/index.d.ts +389 -21
- package/dist/index.mjs +977 -147
- package/dist/shared.cjs +349 -0
- package/dist/shared.mjs +340 -0
- package/dist/worker-thread.mjs +429 -0
- package/dist/worker.d.ts +117 -0
- package/dist/worker.mjs +953 -0
- package/package.json +64 -26
package/dist/index.cjs
CHANGED
|
@@ -3,60 +3,356 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
6
|
+
Object.defineProperty(exports, "Clustering", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function get() {
|
|
9
|
+
return _shared.Clustering;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "Palette", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _shared.Palette;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
exports.quantizeWithPalette = exports.quantizeToIndicesWithPalette = exports.quantizeToIndicesFromClustering = exports.quantizeToIndices = exports.quantizeFromClustering = exports.quantize = exports.init = exports.getSwatchesFromClustering = exports.getSwatches = exports.getPaletteFromClustering = exports.getPalette = exports.getClusteringInfo = exports.getClustering = exports.formatVersion = void 0;
|
|
19
|
+
Object.defineProperty(exports, "version", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function get() {
|
|
22
|
+
return _shared.version;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
7
25
|
var _clustering = _interopRequireDefault(require("./clustering.js"));
|
|
26
|
+
var _shared = require("./shared.cjs");
|
|
8
27
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
9
28
|
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
10
|
-
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
11
|
-
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
12
29
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
13
30
|
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (c = i[4] || 3, u = i[5] === e ? i[3] : i[5], i[4] = 3, i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
|
14
31
|
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); }
|
|
15
32
|
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
16
33
|
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
34
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
35
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
36
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
37
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
38
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
39
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } // The library's version, the stored-data format version, and the Palette and Clustering types (see shared.js).
|
|
40
|
+
// The version of stored data (clusterings) this build writes. It reads this one and format 2 (see readHeader). Unlike
|
|
41
|
+
// `version`, it changes only when the stored format does, so a 3.x release that keeps the format keeps this number.
|
|
42
|
+
var formatVersion = exports.formatVersion = _shared.latestVersion;
|
|
17
43
|
var codes = {
|
|
18
44
|
'rgba': 0,
|
|
19
45
|
'rgb': 1
|
|
20
46
|
};
|
|
21
|
-
var colorSpaces = {
|
|
22
|
-
'rgb': 0,
|
|
23
|
-
'oklab': 1
|
|
24
|
-
};
|
|
25
47
|
var alphaModes = {
|
|
26
48
|
'ignore': 0,
|
|
27
49
|
'weight': 1
|
|
28
50
|
};
|
|
29
51
|
var Module = null;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
52
|
+
|
|
53
|
+
// Turns init's `wasm` option into the settings the Emscripten glue understands. The glue finds its .wasm file next to
|
|
54
|
+
// itself by default (`new URL('clustering.wasm', import.meta.url)`); this lets a caller say where it is, or hand over
|
|
55
|
+
// the bytes or an already compiled module, for CDNs, strict CSPs and runtimes (such as Cloudflare Workers) that
|
|
56
|
+
// cannot fetch or compile it that way. `onError` receives instantiation errors, which the glue would otherwise swallow.
|
|
57
|
+
var wasmSettings = function wasmSettings(wasm, onError) {
|
|
58
|
+
if (wasm === undefined) {
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
if (typeof wasm === 'string' || wasm instanceof URL) {
|
|
62
|
+
var location = String(wasm);
|
|
63
|
+
return {
|
|
64
|
+
locateFile: function locateFile(path, directory) {
|
|
65
|
+
return path.endsWith('.wasm') ? location : directory + path;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// Bytes and compiled modules go through `instantiateWasm`: this build of the glue reads no `wasmBinary` setting.
|
|
70
|
+
if ((0, _shared.isArrayBufferLike)(wasm) || ArrayBuffer.isView(wasm)) {
|
|
71
|
+
var source = (0, _shared.isArrayBufferLike)(wasm) ? wasm : new Uint8Array(wasm.buffer, wasm.byteOffset, wasm.byteLength);
|
|
72
|
+
return {
|
|
73
|
+
instantiateWasm: function instantiateWasm(imports, receive) {
|
|
74
|
+
WebAssembly.instantiate(source, imports).then(function (_ref) {
|
|
75
|
+
var instance = _ref.instance;
|
|
76
|
+
return receive(instance);
|
|
77
|
+
}, onError);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (typeof WebAssembly !== 'undefined' && wasm instanceof WebAssembly.Module) {
|
|
82
|
+
return {
|
|
83
|
+
instantiateWasm: function instantiateWasm(imports, receive) {
|
|
84
|
+
WebAssembly.instantiate(wasm, imports).then(receive, onError);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid wasm: must be a URL or path string, a URL, an ArrayBuffer or typed array of the .wasm bytes, or a WebAssembly.Module.", 'options.wasm');
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// A module that cannot be loaded is the commonest first-run failure: a 404, a Content-Security-Policy that blocks it, a
|
|
92
|
+
// browser without WebAssembly SIMD. The engine's own error (a CompileError, a failed fetch) says little about which.
|
|
93
|
+
var wasmLoadFailed = function wasmLoadFailed(cause) {
|
|
94
|
+
return Object.assign((0, _shared.fail)('wasm-load-failed', "Could not load the WebAssembly module (".concat(String(cause && cause.message !== undefined ? cause.message : cause).split('\n')[0].slice(0, 140), "). It needs WebAssembly with SIMD (Chrome and Edge 91, Firefox 89, Safari 16.4 or newer). If the file is missing or blocked, check that the .wasm is served and allowed by the page's Content-Security-Policy, or say where it is with init({ wasm }).")), {
|
|
95
|
+
cause: cause
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// The .wasm and the script that drives it are built together, and the byte layouts and entry points between them are
|
|
100
|
+
// not a public interface. One left over from another version (a stale cache, a CDN serving an old file, an `init({ wasm })`
|
|
101
|
+
// pointing at the wrong one) would misbehave in ways nothing explains, so the module says which version it is and a
|
|
102
|
+
// mismatch is reported when it loads.
|
|
103
|
+
var checkWasmVersion = function checkWasmVersion(instance) {
|
|
104
|
+
var found = 'an unknown version (from before 3.0)';
|
|
105
|
+
if (typeof instance._library_version === 'function') {
|
|
106
|
+
var heap = instance.HEAPU8;
|
|
107
|
+
var end = instance._library_version();
|
|
108
|
+
var start = end;
|
|
109
|
+
while (heap[end] !== 0 && end - start < 64) {
|
|
110
|
+
end++;
|
|
111
|
+
}
|
|
112
|
+
found = String.fromCharCode.apply(String, _toConsumableArray(heap.subarray(start, end)));
|
|
113
|
+
}
|
|
114
|
+
var expected = "agglomerative-clustering@".concat(_shared.version);
|
|
115
|
+
if (found !== expected) {
|
|
116
|
+
throw (0, _shared.fail)('wasm-load-failed', "The WebAssembly module is ".concat(found, ", but this script is ").concat(expected, ": a stale or mixed-up clustering.wasm (a cache or CDN serving another version, or init({ wasm }) pointing at one). Serve the .wasm that came with this version of the package."));
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// The module loads once: concurrent calls share one load, and a load that fails is forgotten, so it can be retried.
|
|
121
|
+
var loading = null;
|
|
122
|
+
|
|
123
|
+
// Calls get the instance they run on from `acquire` and keep using it, so replacing the current instance (see `discard`)
|
|
124
|
+
// never changes the module under a call already in flight.
|
|
125
|
+
var acquire = /*#__PURE__*/function () {
|
|
126
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(options) {
|
|
127
|
+
var _i, _Object$keys, key, reject, failed, settings;
|
|
128
|
+
return _regenerator().w(function (_context2) {
|
|
129
|
+
while (1) switch (_context2.n) {
|
|
34
130
|
case 0:
|
|
35
|
-
if (
|
|
36
|
-
|
|
131
|
+
if (options === undefined) {
|
|
132
|
+
options = {};
|
|
133
|
+
}
|
|
134
|
+
if ((0, _shared.isOptionsObject)(options)) {
|
|
135
|
+
_context2.n = 1;
|
|
37
136
|
break;
|
|
38
137
|
}
|
|
39
|
-
|
|
40
|
-
return (0, _clustering["default"])();
|
|
138
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid options: expected an object.", 'options');
|
|
41
139
|
case 1:
|
|
42
|
-
|
|
140
|
+
options = (0, _shared.snapshot)(options);
|
|
141
|
+
_i = 0, _Object$keys = Object.keys(options);
|
|
43
142
|
case 2:
|
|
44
|
-
|
|
143
|
+
if (!(_i < _Object$keys.length)) {
|
|
144
|
+
_context2.n = 4;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
key = _Object$keys[_i];
|
|
148
|
+
if (!(key !== 'wasm')) {
|
|
149
|
+
_context2.n = 3;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid option '".concat(key, "': expected wasm."), "options.".concat(key));
|
|
153
|
+
case 3:
|
|
154
|
+
_i++;
|
|
155
|
+
_context2.n = 2;
|
|
156
|
+
break;
|
|
157
|
+
case 4:
|
|
158
|
+
failed = new Promise(function (_, r) {
|
|
159
|
+
reject = r;
|
|
160
|
+
});
|
|
161
|
+
failed["catch"](function () {});
|
|
162
|
+
settings = wasmSettings(options.wasm, reject);
|
|
163
|
+
if (!Module) {
|
|
164
|
+
_context2.n = 5;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
return _context2.a(2, Module);
|
|
168
|
+
case 5:
|
|
169
|
+
if (!loading) {
|
|
170
|
+
loading = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
171
|
+
var instance;
|
|
172
|
+
return _regenerator().w(function (_context) {
|
|
173
|
+
while (1) switch (_context.n) {
|
|
174
|
+
case 0:
|
|
175
|
+
_context.n = 1;
|
|
176
|
+
return Promise.race([(0, _clustering["default"])(settings), failed])["catch"](function (error) {
|
|
177
|
+
throw wasmLoadFailed(error);
|
|
178
|
+
});
|
|
179
|
+
case 1:
|
|
180
|
+
instance = _context.v;
|
|
181
|
+
checkWasmVersion(instance);
|
|
182
|
+
Module = instance;
|
|
183
|
+
return _context.a(2, instance);
|
|
184
|
+
}
|
|
185
|
+
}, _callee);
|
|
186
|
+
}))()["catch"](function (error) {
|
|
187
|
+
loading = null;
|
|
188
|
+
throw error;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
return _context2.a(2, loading);
|
|
192
|
+
}
|
|
193
|
+
}, _callee2);
|
|
194
|
+
}));
|
|
195
|
+
return function acquire(_x) {
|
|
196
|
+
return _ref2.apply(this, arguments);
|
|
197
|
+
};
|
|
198
|
+
}();
|
|
199
|
+
|
|
200
|
+
// Loads the WebAssembly module. Every other function does this on first use, so calling it is optional, except to
|
|
201
|
+
// say where the .wasm file is (`wasm`), which must come before anything else. The first load that succeeds wins:
|
|
202
|
+
// later calls, with or without options, get the same module.
|
|
203
|
+
var init = exports.init = /*#__PURE__*/function () {
|
|
204
|
+
var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(options) {
|
|
205
|
+
return _regenerator().w(function (_context3) {
|
|
206
|
+
while (1) switch (_context3.n) {
|
|
207
|
+
case 0:
|
|
208
|
+
_context3.n = 1;
|
|
209
|
+
return acquire(options);
|
|
210
|
+
case 1:
|
|
211
|
+
return _context3.a(2);
|
|
45
212
|
}
|
|
46
|
-
},
|
|
213
|
+
}, _callee3);
|
|
47
214
|
}));
|
|
48
|
-
return function init() {
|
|
49
|
-
return
|
|
215
|
+
return function init(_x2) {
|
|
216
|
+
return _ref4.apply(this, arguments);
|
|
50
217
|
};
|
|
51
218
|
}();
|
|
52
|
-
|
|
53
|
-
|
|
219
|
+
|
|
220
|
+
// A WebAssembly abort, which is what running out of memory looks like, leaves the instance's heap full of whatever the
|
|
221
|
+
// failed call had allocated and never freed (the abort skips destructors), so every later large call on it would fail
|
|
222
|
+
// too. The instance is therefore discarded: the next call loads a fresh one, and calls already running keep theirs.
|
|
223
|
+
var isAbort = function isAbort(error) {
|
|
224
|
+
return error instanceof Error && error.name === 'RuntimeError' && /^Aborted\(/.test(error.message);
|
|
225
|
+
};
|
|
226
|
+
var discard = function discard(instance) {
|
|
227
|
+
if (Module === instance) {
|
|
228
|
+
Module = null;
|
|
229
|
+
loading = null;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
var outOfMemory = function outOfMemory() {
|
|
233
|
+
return (0, _shared.fail)('out-of-memory', "The WebAssembly module ran out of memory (it can use up to 2 GB for one image). Try `precision: 6`, which bounds the memory an image with very many colours needs, or a smaller image.");
|
|
234
|
+
};
|
|
235
|
+
var unpack = function unpack(wasm, pointer) {
|
|
236
|
+
var lengthBuffer = new Uint8Array(wasm.HEAPU8.subarray(pointer, pointer + 4));
|
|
54
237
|
var length = lengthBuffer[0] | lengthBuffer[1] << 8 | lengthBuffer[2] << 16 | lengthBuffer[3] << 24;
|
|
55
|
-
var outputBuffer = new Uint8Array(
|
|
238
|
+
var outputBuffer = new Uint8Array(wasm.HEAPU8.subarray(pointer + 4, pointer + 4 + length));
|
|
56
239
|
return outputBuffer;
|
|
57
240
|
};
|
|
241
|
+
|
|
242
|
+
// A Uint8Array over exactly the same bytes, so views into a larger buffer (subarrays) stay views of their own range.
|
|
243
|
+
var bytes = function bytes(data) {
|
|
244
|
+
return (0, _shared.isUint8Array)(data) ? data : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// ----- Encoded image files -------------------------------------------------------------------------------------------
|
|
248
|
+
// A bare Uint8Array is taken to be raw RGBA pixels, so the commonest mistake is handing over the bytes of an image *file*
|
|
249
|
+
// (fs.readFile, fetch().arrayBuffer()): it passes as pixels whenever its length is a multiple of 4 and gives a palette of
|
|
250
|
+
// nonsense. The signatures of the common formats are recognised so that it is reported instead. Only a bare Uint8Array is
|
|
251
|
+
// looked at: an ImageData or a { data, format } object has said what its bytes are, so it is never second-guessed, and a
|
|
252
|
+
// bare array that really is raw pixels can be passed as { data, format: 'rgba' } to say so.
|
|
253
|
+
//
|
|
254
|
+
// This is also where decoding would go. The library doesn't decode files today (that needs a decoder per format, and
|
|
255
|
+
// sharp, pngjs, createImageBitmap and the like already do it); when it does, a file recognised here is decoded into
|
|
256
|
+
// { data, format, width, height } instead of rejected, and `load` becomes asynchronous. See docs/ROADMAP.md.
|
|
257
|
+
var ascii = function ascii(bytes, at, text) {
|
|
258
|
+
for (var i = 0; i < text.length; i++) {
|
|
259
|
+
if (bytes[at + i] !== text.charCodeAt(i)) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return true;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
// The name of the encoded image format `bytes` starts with, or null. Signatures are long enough, or have enough structure
|
|
267
|
+
// beyond the first few bytes, that raw pixels all but never match.
|
|
268
|
+
var sniffImageFormat = function sniffImageFormat(bytes) {
|
|
269
|
+
var length = bytes.length;
|
|
270
|
+
if (length >= 8 && bytes[0] === 0x89 && ascii(bytes, 1, 'PNG') && bytes[4] === 0x0D && bytes[5] === 0x0A && bytes[6] === 0x1A && bytes[7] === 0x0A) {
|
|
271
|
+
return 'PNG';
|
|
272
|
+
}
|
|
273
|
+
// JPEG: FF D8, then a first segment: a marker that has a length (not a restart or a stuffed byte) and a length that fits
|
|
274
|
+
if (length >= 6 && bytes[0] === 0xFF && bytes[1] === 0xD8 && bytes[2] === 0xFF && bytes[3] >= 0xC0 && bytes[3] <= 0xFE && !(bytes[3] >= 0xD0 && bytes[3] <= 0xD9) && (bytes[4] << 8 | bytes[5]) >= 2 && 4 + (bytes[4] << 8 | bytes[5]) <= length) {
|
|
275
|
+
return 'JPEG';
|
|
276
|
+
}
|
|
277
|
+
if (length >= 6 && (ascii(bytes, 0, 'GIF87a') || ascii(bytes, 0, 'GIF89a'))) {
|
|
278
|
+
return 'GIF';
|
|
279
|
+
}
|
|
280
|
+
if (length >= 12 && ascii(bytes, 0, 'RIFF') && ascii(bytes, 8, 'WEBP')) {
|
|
281
|
+
return 'WebP';
|
|
282
|
+
}
|
|
283
|
+
// BMP: 'BM', then the file's own length and two zero reserved fields, and a known header size
|
|
284
|
+
if (length >= 18 && bytes[0] === 0x42 && bytes[1] === 0x4D && bytes[6] === 0 && bytes[7] === 0 && bytes[8] === 0 && bytes[9] === 0 && (bytes[2] | bytes[3] << 8 | bytes[4] << 16 | bytes[5] << 24) === length && [12, 40, 52, 56, 64, 108, 124].includes(bytes[14]) && bytes[15] === 0 && bytes[16] === 0 && bytes[17] === 0) {
|
|
285
|
+
return 'BMP';
|
|
286
|
+
}
|
|
287
|
+
// TIFF: a byte order mark and 42, then the offset of the first directory, which must lie in the file and start with a plausible entry count
|
|
288
|
+
if (length >= 12) {
|
|
289
|
+
var little = bytes[0] === 0x49 && bytes[1] === 0x49 && bytes[2] === 0x2A && bytes[3] === 0;
|
|
290
|
+
var big = bytes[0] === 0x4D && bytes[1] === 0x4D && bytes[2] === 0 && bytes[3] === 0x2A;
|
|
291
|
+
if (little || big) {
|
|
292
|
+
var u16 = function u16(at) {
|
|
293
|
+
return little ? bytes[at] | bytes[at + 1] << 8 : bytes[at] << 8 | bytes[at + 1];
|
|
294
|
+
};
|
|
295
|
+
var offset = little ? (bytes[4] | bytes[5] << 8 | bytes[6] << 16 | bytes[7] << 24) >>> 0 : (bytes[4] << 24 | bytes[5] << 16 | bytes[6] << 8 | bytes[7]) >>> 0;
|
|
296
|
+
if (offset >= 8 && offset + 2 <= length && u16(offset) >= 1 && u16(offset) <= 4096) {
|
|
297
|
+
return 'TIFF';
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// ISO base media: a 'ftyp' box, then a brand
|
|
302
|
+
if (length >= 12 && ascii(bytes, 4, 'ftyp')) {
|
|
303
|
+
if (ascii(bytes, 8, 'avif') || ascii(bytes, 8, 'avis')) {
|
|
304
|
+
return 'AVIF';
|
|
305
|
+
}
|
|
306
|
+
if (['heic', 'heix', 'hevc', 'hevx', 'heim', 'heis', 'mif1', 'msf1'].some(function (brand) {
|
|
307
|
+
return ascii(bytes, 8, brand);
|
|
308
|
+
})) {
|
|
309
|
+
return 'HEIC';
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return null;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// What to say about something that isn't an image, when it is a common thing to pass by mistake: each has a one-line fix.
|
|
316
|
+
var imageHint = function imageHint(image) {
|
|
317
|
+
var decode = " Decode it to pixels first, then pass them.";
|
|
318
|
+
if (image === undefined || image === null) {
|
|
319
|
+
return " Got ".concat(image, ".");
|
|
320
|
+
}
|
|
321
|
+
if (typeof image === 'string') {
|
|
322
|
+
return " Got a string (a file name, URL or data URL?): load and decode it to pixels first.";
|
|
323
|
+
}
|
|
324
|
+
if (_typeof(image) !== 'object') {
|
|
325
|
+
return " Got a ".concat(_typeof(image), ".");
|
|
326
|
+
}
|
|
327
|
+
if (Array.isArray(image)) {
|
|
328
|
+
return " Got a plain array: use new Uint8Array(array) for RGBA bytes, or { data: new Uint8Array(array), format: 'rgb' }.";
|
|
329
|
+
}
|
|
330
|
+
if ((0, _shared.isArrayBufferLike)(image)) {
|
|
331
|
+
return " Got an ArrayBuffer, which has no pixel layout: wrap it, new Uint8Array(buffer) for RGBA bytes (if it holds an encoded image file, decode it first).";
|
|
332
|
+
}
|
|
333
|
+
if (typeof image.getContext === 'function') {
|
|
334
|
+
return " Got a canvas: pass its pixels, canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height).";
|
|
335
|
+
}
|
|
336
|
+
if (typeof image.arrayBuffer === 'function' && typeof image.type === 'string') {
|
|
337
|
+
return " Got a Blob or File, which is an encoded image:" + decode + " (createImageBitmap(blob), draw it to a canvas, then getImageData()).";
|
|
338
|
+
}
|
|
339
|
+
if (typeof image.close === 'function' || typeof image.nodeName === 'string') {
|
|
340
|
+
return " Got an image element or ImageBitmap: draw it to a canvas (or OffscreenCanvas) and pass getImageData().";
|
|
341
|
+
}
|
|
342
|
+
if (image.data && !image.format) {
|
|
343
|
+
return " It has `data` but no `format`: add format: 'rgba' (or 'rgb') to use it as a RawImage.";
|
|
344
|
+
}
|
|
345
|
+
return '';
|
|
346
|
+
};
|
|
58
347
|
var load = function load(image) {
|
|
59
|
-
if (
|
|
348
|
+
if ((0, _shared.isUint8Array)(image)) {
|
|
349
|
+
var encoded = sniffImageFormat(image);
|
|
350
|
+
if (encoded) {
|
|
351
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid image: these bytes are an encoded ".concat(encoded, " file, not pixels. Decode it first (in a browser, draw it to a canvas or use createImageBitmap, then pass getImageData(); in Node use sharp, pngjs or similar) and pass the raw pixels. If they really are raw RGBA pixels, pass { data, format: 'rgba' } to say so."), 'image');
|
|
352
|
+
}
|
|
353
|
+
if (image.length % 4 !== 0) {
|
|
354
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid image data: length must be a multiple of 4 for 'rgba'.", 'image');
|
|
355
|
+
}
|
|
60
356
|
return {
|
|
61
357
|
data: image,
|
|
62
358
|
format: 'rgba'
|
|
@@ -64,35 +360,59 @@ var load = function load(image) {
|
|
|
64
360
|
}
|
|
65
361
|
if (typeof ImageData !== 'undefined' && image instanceof ImageData) {
|
|
66
362
|
return {
|
|
67
|
-
data:
|
|
363
|
+
data: bytes(image.data),
|
|
68
364
|
format: 'rgba',
|
|
69
365
|
width: image.width
|
|
70
366
|
};
|
|
71
367
|
}
|
|
368
|
+
if (ArrayBuffer.isView(image)) {
|
|
369
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid image: a bare typed array must be a Uint8Array of RGBA bytes. For a Uint8ClampedArray such as imageData.data, pass the ImageData itself or { data, format: 'rgba' }.", 'image');
|
|
370
|
+
}
|
|
72
371
|
if (_typeof(image) !== 'object' || !image || !image.data || !image.format) {
|
|
73
|
-
throw
|
|
372
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid image: expected object with `data` and `format`.".concat(imageHint(image)), 'image');
|
|
74
373
|
}
|
|
75
|
-
if (!(image.data
|
|
76
|
-
throw
|
|
374
|
+
if (!(0, _shared.isUint8Array)(image.data) && !(0, _shared.isUint8ClampedArray)(image.data)) {
|
|
375
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid image data: must be Uint8Array or Uint8ClampedArray.", 'image');
|
|
77
376
|
}
|
|
78
377
|
if (!Object.keys(codes).includes(image.format)) {
|
|
79
|
-
throw
|
|
378
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid format: must be 'rgb' or 'rgba'.", 'image');
|
|
379
|
+
}
|
|
380
|
+
var channels = image.format === 'rgb' ? 3 : 4;
|
|
381
|
+
if (image.data.length % channels !== 0) {
|
|
382
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid image data: length must be a multiple of ".concat(channels, " for '").concat(image.format, "'."), 'image');
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// width and height are copied to results and used for dithering, so they must make sense: whole numbers that, when
|
|
386
|
+
// both are given, account for exactly the pixels in the data.
|
|
387
|
+
for (var _i2 = 0, _arr = ['width', 'height']; _i2 < _arr.length; _i2++) {
|
|
388
|
+
var key = _arr[_i2];
|
|
389
|
+
var value = image[key];
|
|
390
|
+
if (value !== undefined && (!Number.isInteger(value) || value < 0)) {
|
|
391
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid image ".concat(key, ": must be a whole number of pixels."), 'image');
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (image.width !== undefined && image.height !== undefined && image.width * image.height !== image.data.length / channels) {
|
|
395
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid image: width x height (".concat(image.width, " x ").concat(image.height, ") does not match its ").concat(image.data.length / channels, " pixels."), 'image');
|
|
80
396
|
}
|
|
81
397
|
return {
|
|
82
|
-
data:
|
|
398
|
+
data: bytes(image.data),
|
|
83
399
|
format: image.format,
|
|
84
400
|
width: image.width
|
|
85
401
|
};
|
|
86
402
|
};
|
|
87
403
|
var getWrapper = function getWrapper(image) {
|
|
88
404
|
if (typeof ImageData !== 'undefined' && image instanceof ImageData) {
|
|
405
|
+
// The pixels stay in the colour space they came in ('display-p3' canvases), so the result must say so.
|
|
89
406
|
var width = image.width,
|
|
90
|
-
height = image.height
|
|
407
|
+
height = image.height,
|
|
408
|
+
colorSpace = image.colorSpace;
|
|
91
409
|
return function (output) {
|
|
92
|
-
return new ImageData(new Uint8ClampedArray(output.buffer), width, height
|
|
410
|
+
return new ImageData(new Uint8ClampedArray(output.buffer), width, height, colorSpace ? {
|
|
411
|
+
colorSpace: colorSpace
|
|
412
|
+
} : undefined);
|
|
93
413
|
};
|
|
94
414
|
}
|
|
95
|
-
if (!(
|
|
415
|
+
if (!(0, _shared.isUint8Array)(image) && _typeof(image) === 'object' && image && image.data && image.format) {
|
|
96
416
|
var format = image.format,
|
|
97
417
|
_width = image.width,
|
|
98
418
|
_height = image.height;
|
|
@@ -114,10 +434,48 @@ var getWrapper = function getWrapper(image) {
|
|
|
114
434
|
return output;
|
|
115
435
|
};
|
|
116
436
|
};
|
|
437
|
+
|
|
438
|
+
// Decodes _quantize_indices output: [entries, transparent index, index bytes] as 32-bit little-endian values, then
|
|
439
|
+
// the palette, then the indices. `image` is the caller's original input, for its width and height.
|
|
440
|
+
var unpackIndices = function unpackIndices(output, image, colorSpace) {
|
|
441
|
+
var view = new DataView(output.buffer, output.byteOffset, output.byteLength);
|
|
442
|
+
var entries = view.getUint32(0, true);
|
|
443
|
+
var transparent = view.getInt32(4, true);
|
|
444
|
+
var indexBytes = view.getUint32(8, true);
|
|
445
|
+
var palette = (0, _shared.markPalette)(output.slice(12, 12 + 3 * entries), colorSpace);
|
|
446
|
+
var start = 12 + 3 * entries;
|
|
447
|
+
var count = (output.length - start) / indexBytes;
|
|
448
|
+
var Indices = indexBytes === 1 ? Uint8Array : indexBytes === 2 ? Uint16Array : Uint32Array;
|
|
449
|
+
var indices = new Indices(count);
|
|
450
|
+
// Little-endian, like the WASM heap, which Emscripten's own typed-array views already assume of the host.
|
|
451
|
+
new Uint8Array(indices.buffer).set(output.subarray(start));
|
|
452
|
+
// `colors` is the palette without the transparent entry, ready to map with again (the entry is black and takes no part in matching)
|
|
453
|
+
var colors = transparent < 0 ? palette : (0, _shared.markPalette)(palette.slice(0, 3 * transparent), colorSpace);
|
|
454
|
+
var result = {
|
|
455
|
+
palette: palette,
|
|
456
|
+
colors: colors,
|
|
457
|
+
indices: indices,
|
|
458
|
+
indexBits: 8 * indexBytes,
|
|
459
|
+
transparentIndex: transparent < 0 ? null : transparent
|
|
460
|
+
};
|
|
461
|
+
if (!(0, _shared.isUint8Array)(image)) {
|
|
462
|
+
if (image.width !== undefined) {
|
|
463
|
+
result.width = image.width;
|
|
464
|
+
}
|
|
465
|
+
if (image.height !== undefined) {
|
|
466
|
+
result.height = image.height;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return result;
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
// Returns k, validated. Any k above the number of colours means all of them, so it is capped at the largest value
|
|
473
|
+
// the WASM side holds (a larger one would overflow its int).
|
|
117
474
|
var check = function check(k) {
|
|
118
475
|
if (typeof k !== 'number' || !Number.isInteger(k) || k <= 0) {
|
|
119
|
-
throw
|
|
476
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid k: must be a strictly positive integer.", 'k');
|
|
120
477
|
}
|
|
478
|
+
return Math.min(k, 0x7FFFFFFF);
|
|
121
479
|
};
|
|
122
480
|
|
|
123
481
|
// Validates `options` against the keys a function accepts and fills in defaults:
|
|
@@ -127,88 +485,121 @@ var check = function check(k) {
|
|
|
127
485
|
// precision: bits per channel, 1-8 (default 8, exact); lower values group similar colours before clustering
|
|
128
486
|
// dither: false (default), true, or a strength in [0, 1]; error diffusion when mapping pixels
|
|
129
487
|
// width: image width in pixels, needed to dither a Uint8Array or a RawImage without `width`
|
|
488
|
+
// signal, onProgress: an AbortSignal and a progress callback, which make the clustering run in slices (see clusterInSlices)
|
|
489
|
+
// 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);
|
|
490
|
+
// indexed output also gives them their own entry, which counts towards k
|
|
491
|
+
|
|
130
492
|
var parseOptions = function parseOptions(options, allowed, image) {
|
|
131
493
|
if (options === undefined) {
|
|
132
494
|
options = {};
|
|
133
495
|
}
|
|
134
|
-
if (
|
|
135
|
-
throw
|
|
496
|
+
if (!(0, _shared.isOptionsObject)(options)) {
|
|
497
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid options: expected an object.", 'options');
|
|
136
498
|
}
|
|
137
|
-
|
|
138
|
-
|
|
499
|
+
options = (0, _shared.snapshot)(options);
|
|
500
|
+
for (var _i3 = 0, _Object$keys2 = Object.keys(options); _i3 < _Object$keys2.length; _i3++) {
|
|
501
|
+
var key = _Object$keys2[_i3];
|
|
139
502
|
if (!allowed.includes(key)) {
|
|
140
|
-
throw
|
|
503
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid option '".concat(key, "': expected one of ").concat(allowed.join(', '), "."), "options.".concat(key));
|
|
141
504
|
}
|
|
142
505
|
}
|
|
143
506
|
var colorSpace = options.colorSpace === undefined ? 'rgb' : options.colorSpace;
|
|
144
|
-
if (!Object.keys(colorSpaces).includes(colorSpace)) {
|
|
145
|
-
throw
|
|
507
|
+
if (!Object.keys(_shared.colorSpaces).includes(colorSpace)) {
|
|
508
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid colorSpace: must be 'rgb' or 'oklab'.", 'options.colorSpace');
|
|
146
509
|
}
|
|
147
510
|
var alpha = options.alpha === undefined ? 'weight' : options.alpha;
|
|
148
511
|
if (!Object.keys(alphaModes).includes(alpha)) {
|
|
149
|
-
throw
|
|
512
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid alpha: must be 'weight' or 'ignore'.", 'options.alpha');
|
|
150
513
|
}
|
|
151
514
|
var precision = options.precision === undefined ? 8 : options.precision;
|
|
152
515
|
if (typeof precision !== 'number' || !Number.isInteger(precision) || precision < 1 || precision > 8) {
|
|
153
|
-
throw
|
|
516
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid precision: must be an integer from 1 to 8.", 'options.precision');
|
|
154
517
|
}
|
|
155
518
|
var dither = options.dither === undefined || options.dither === false ? 0 : options.dither === true ? 1 : options.dither;
|
|
156
519
|
if (typeof dither !== 'number' || !Number.isFinite(dither) || dither < 0 || dither > 1) {
|
|
157
|
-
throw
|
|
520
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid dither: must be a boolean or a number from 0 to 1.", 'options.dither');
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// The image's own width and options.width are two sources for one fact: they may repeat each other, but not disagree
|
|
524
|
+
// (a wrong row length would dither without an error, as long as it divides the pixel count).
|
|
525
|
+
if (options.width !== undefined && image && image.width !== undefined && options.width !== image.width) {
|
|
526
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid width: options.width (".concat(options.width, ") does not match the image's width (").concat(image.width, ")."), 'options.width');
|
|
158
527
|
}
|
|
159
528
|
var width = options.width !== undefined ? options.width : image ? image.width : undefined;
|
|
160
529
|
if (dither > 0) {
|
|
161
530
|
var channels = image.format === 'rgb' ? 3 : 4;
|
|
162
531
|
if (typeof width !== 'number' || !Number.isInteger(width) || width <= 0) {
|
|
163
|
-
throw
|
|
532
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid width: dithering needs the image width, as `options.width` or the image's `width`.", 'options.width');
|
|
164
533
|
}
|
|
165
534
|
if (image.data.length / channels % width !== 0) {
|
|
166
|
-
throw
|
|
535
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid width: the image's pixel count is not a multiple of it.", 'options.width');
|
|
167
536
|
}
|
|
168
537
|
}
|
|
538
|
+
var signal = options.signal;
|
|
539
|
+
if (signal !== undefined && (signal === null || _typeof(signal) !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function')) {
|
|
540
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid signal: must be an AbortSignal.", 'options.signal');
|
|
541
|
+
}
|
|
542
|
+
var onProgress = options.onProgress;
|
|
543
|
+
if (onProgress !== undefined && typeof onProgress !== 'function') {
|
|
544
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid onProgress: must be a function.", 'options.onProgress');
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// The alpha cutoff for indexed output: 0 for none, else the pixels with alpha below it are transparent.
|
|
548
|
+
var transparentOption = options.transparent === undefined ? false : options.transparent;
|
|
549
|
+
var transparent = transparentOption === false ? 0 : transparentOption === true ? 128 : transparentOption;
|
|
550
|
+
if (!Number.isInteger(transparent) || transparent < 0 || transparent > 255 || transparent === 0 && transparentOption !== false) {
|
|
551
|
+
throw (0, _shared.fail)('invalid-argument', "Invalid transparent: must be a boolean or an integer alpha cutoff from 1 to 255.", 'options.transparent');
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// A cutoff replaces opacity weighting (pixels count whole or not at all), so naming both would leave one of them ignored.
|
|
555
|
+
if (transparent > 0 && options.alpha !== undefined) {
|
|
556
|
+
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');
|
|
557
|
+
}
|
|
169
558
|
return {
|
|
170
|
-
colorSpace: colorSpaces[colorSpace],
|
|
559
|
+
colorSpace: _shared.colorSpaces[colorSpace],
|
|
171
560
|
colorSpaceGiven: options.colorSpace !== undefined,
|
|
172
561
|
alpha: alphaModes[alpha],
|
|
173
562
|
precision: precision,
|
|
174
563
|
dither: dither,
|
|
175
|
-
width: dither > 0 ? width : 0
|
|
564
|
+
width: dither > 0 ? width : 0,
|
|
565
|
+
transparent: transparent,
|
|
566
|
+
transparentGiven: options.transparent !== undefined,
|
|
567
|
+
signal: signal,
|
|
568
|
+
onProgress: onProgress
|
|
176
569
|
};
|
|
177
570
|
};
|
|
178
|
-
var
|
|
179
|
-
var
|
|
180
|
-
var
|
|
571
|
+
var controlOptions = ['signal', 'onProgress'];
|
|
572
|
+
var clusterOptions = ['colorSpace', 'precision', 'alpha', 'transparent'].concat(controlOptions);
|
|
573
|
+
var quantizeOptions = [].concat(_toConsumableArray(clusterOptions), ['dither', 'width']);
|
|
574
|
+
// The functions that only map pixels take just `signal`: they finish in milliseconds, so there is nothing to report progress
|
|
575
|
+
// on or to interrupt part way; a signal that is already aborted does no work, which is all it can mean.
|
|
576
|
+
var mapOptions = ['colorSpace', 'dither', 'width', 'signal'];
|
|
577
|
+
var quantizeIndexOptions = quantizeOptions;
|
|
578
|
+
var mapIndexOptions = [].concat(mapOptions, ['transparent']);
|
|
181
579
|
|
|
182
|
-
//
|
|
183
|
-
var
|
|
184
|
-
|
|
185
|
-
throw new Error("Invalid clustering: must be a Uint8Array from getClustering.");
|
|
186
|
-
}
|
|
187
|
-
var rest = clustering.length % 9;
|
|
188
|
-
var legacy = clustering.length === 0 || rest === 3;
|
|
189
|
-
var current = (clustering.length === 4 || rest === 7) && clustering[0] === 0xAC && clustering[1] === 2 && clustering[2] <= 1;
|
|
190
|
-
if (!legacy && !current) {
|
|
191
|
-
throw new Error("Invalid clustering: not a clustering from getClustering.");
|
|
192
|
-
}
|
|
580
|
+
// The space to match pixels to a palette in: the one the caller names, else the one a Palette was made in, else RGB.
|
|
581
|
+
var matchingSpace = function matchingSpace(o, palette) {
|
|
582
|
+
return o.colorSpaceGiven ? o.colorSpace : _shared.colorSpaces[(0, _shared.isPalette)(palette) ? palette.colorSpace : 'rgb'];
|
|
193
583
|
};
|
|
194
584
|
|
|
195
585
|
// Copies each buffer into the WASM heap, calls `fn` with their pointers, and returns the unpacked output,
|
|
196
586
|
// freeing everything afterwards.
|
|
197
|
-
var call = function call(buffers, fn) {
|
|
587
|
+
var call = function call(wasm, buffers, fn) {
|
|
198
588
|
var pointers = [];
|
|
199
589
|
var outputPointer = 0;
|
|
590
|
+
var aborted = false;
|
|
200
591
|
try {
|
|
201
592
|
var _iterator = _createForOfIteratorHelper(buffers),
|
|
202
593
|
_step;
|
|
203
594
|
try {
|
|
204
595
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
205
596
|
var buffer = _step.value;
|
|
206
|
-
var pointer =
|
|
597
|
+
var pointer = wasm._malloc(buffer.length);
|
|
207
598
|
if (!pointer) {
|
|
208
|
-
throw
|
|
599
|
+
throw (0, _shared.fail)('out-of-memory', "Failed to allocate WASM memory.");
|
|
209
600
|
}
|
|
210
601
|
pointers.push(pointer);
|
|
211
|
-
|
|
602
|
+
wasm.HEAPU8.set(buffer, pointer);
|
|
212
603
|
}
|
|
213
604
|
} catch (err) {
|
|
214
605
|
_iterator.e(err);
|
|
@@ -216,147 +607,601 @@ var call = function call(buffers, fn) {
|
|
|
216
607
|
_iterator.f();
|
|
217
608
|
}
|
|
218
609
|
outputPointer = fn.apply(void 0, pointers);
|
|
219
|
-
return unpack(outputPointer);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
Module._free(_pointer);
|
|
610
|
+
return unpack(wasm, outputPointer);
|
|
611
|
+
} catch (error) {
|
|
612
|
+
if (!isAbort(error)) {
|
|
613
|
+
throw error;
|
|
224
614
|
}
|
|
225
|
-
|
|
226
|
-
|
|
615
|
+
aborted = true;
|
|
616
|
+
discard(wasm);
|
|
617
|
+
throw outOfMemory();
|
|
618
|
+
} finally {
|
|
619
|
+
// After an abort the instance is being thrown away, and its allocator can't be trusted.
|
|
620
|
+
if (!aborted) {
|
|
621
|
+
var _iterator2 = _createForOfIteratorHelper(pointers),
|
|
622
|
+
_step2;
|
|
623
|
+
try {
|
|
624
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
625
|
+
var _pointer = _step2.value;
|
|
626
|
+
wasm._free(_pointer);
|
|
627
|
+
}
|
|
628
|
+
} catch (err) {
|
|
629
|
+
_iterator2.e(err);
|
|
630
|
+
} finally {
|
|
631
|
+
_iterator2.f();
|
|
632
|
+
}
|
|
633
|
+
if (outputPointer) {
|
|
634
|
+
wasm._free(outputPointer);
|
|
635
|
+
}
|
|
227
636
|
}
|
|
228
637
|
}
|
|
229
638
|
};
|
|
639
|
+
|
|
640
|
+
// ----- Interruptible work -------------------------------------------------------------------------------------------
|
|
641
|
+
// A call given a `signal` or an `onProgress` runs its clustering in slices of about `sliceMs` milliseconds, giving the
|
|
642
|
+
// event loop a turn between them: the signal is checked (and progress reported) each time, other tasks can run, and
|
|
643
|
+
// in a worker thread the cancel message is read. Without either the call runs in one go, as it always did. The
|
|
644
|
+
// result is the same either way: the slices are the same steps in the same order.
|
|
645
|
+
|
|
646
|
+
var sliceMs = 10;
|
|
647
|
+
|
|
648
|
+
// Gives other tasks (a cancel message, timers, painting) a turn.
|
|
649
|
+
var yieldNow = function yieldNow() {
|
|
650
|
+
if (typeof setImmediate === 'function') {
|
|
651
|
+
return new Promise(function (resolve) {
|
|
652
|
+
return setImmediate(resolve);
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
if (typeof MessageChannel === 'function') {
|
|
656
|
+
return new Promise(function (resolve) {
|
|
657
|
+
var _MessageChannel = new MessageChannel(),
|
|
658
|
+
port1 = _MessageChannel.port1,
|
|
659
|
+
port2 = _MessageChannel.port2;
|
|
660
|
+
port1.onmessage = function () {
|
|
661
|
+
port1.close();
|
|
662
|
+
port2.close();
|
|
663
|
+
resolve();
|
|
664
|
+
};
|
|
665
|
+
port2.postMessage(null);
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
return new Promise(function (resolve) {
|
|
669
|
+
return setTimeout(resolve, 0);
|
|
670
|
+
});
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
// Like fetch, an aborted call rejects with the signal's reason (an AbortError unless the caller gave another).
|
|
674
|
+
var checkAborted = function checkAborted(signal) {
|
|
675
|
+
if (signal && signal.aborted) {
|
|
676
|
+
throw signal.reason !== undefined ? signal.reason : new DOMException('This operation was aborted', 'AbortError');
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
// Reports progress as a fraction from 0 to 1, never going backwards and only when it changes.
|
|
681
|
+
var reporter = function reporter(o) {
|
|
682
|
+
var last = -1;
|
|
683
|
+
return function (fraction) {
|
|
684
|
+
var value = Math.min(1, Math.max(0, fraction));
|
|
685
|
+
if (o.onProgress && value > last) {
|
|
686
|
+
last = value;
|
|
687
|
+
o.onProgress(value);
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
};
|
|
691
|
+
var isControlled = function isControlled(o) {
|
|
692
|
+
return o.signal !== undefined || o.onProgress !== undefined;
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
// Clusters in slices and returns the clustering. `share` is the part of the whole call's progress this takes.
|
|
696
|
+
var clusterInSlices = /*#__PURE__*/function () {
|
|
697
|
+
var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(wasm, image, o, report, share) {
|
|
698
|
+
var imagePointer, job, aborted, progress, pointer, _t;
|
|
699
|
+
return _regenerator().w(function (_context4) {
|
|
700
|
+
while (1) switch (_context4.n) {
|
|
701
|
+
case 0:
|
|
702
|
+
checkAborted(o.signal);
|
|
703
|
+
imagePointer = wasm._malloc(image.data.length);
|
|
704
|
+
if (imagePointer) {
|
|
705
|
+
_context4.n = 1;
|
|
706
|
+
break;
|
|
707
|
+
}
|
|
708
|
+
throw (0, _shared.fail)('out-of-memory', "Failed to allocate WASM memory.");
|
|
709
|
+
case 1:
|
|
710
|
+
job = 0;
|
|
711
|
+
aborted = false;
|
|
712
|
+
_context4.p = 2;
|
|
713
|
+
wasm.HEAPU8.set(image.data, imagePointer);
|
|
714
|
+
job = wasm._clustering_job_start(imagePointer, image.data.length, codes[image.format], o.colorSpace, o.precision, o.alpha, o.transparent);
|
|
715
|
+
report(0);
|
|
716
|
+
case 3:
|
|
717
|
+
if (!true) {
|
|
718
|
+
_context4.n = 6;
|
|
719
|
+
break;
|
|
720
|
+
}
|
|
721
|
+
progress = wasm._clustering_job_step(job, sliceMs);
|
|
722
|
+
if (!(progress >= 1000)) {
|
|
723
|
+
_context4.n = 4;
|
|
724
|
+
break;
|
|
725
|
+
}
|
|
726
|
+
return _context4.a(3, 6);
|
|
727
|
+
case 4:
|
|
728
|
+
report(share * progress / 1000);
|
|
729
|
+
_context4.n = 5;
|
|
730
|
+
return yieldNow();
|
|
731
|
+
case 5:
|
|
732
|
+
checkAborted(o.signal);
|
|
733
|
+
_context4.n = 3;
|
|
734
|
+
break;
|
|
735
|
+
case 6:
|
|
736
|
+
pointer = wasm._clustering_job_result(job);
|
|
737
|
+
_context4.p = 7;
|
|
738
|
+
return _context4.a(2, unpack(wasm, pointer));
|
|
739
|
+
case 8:
|
|
740
|
+
_context4.p = 8;
|
|
741
|
+
wasm._free(pointer);
|
|
742
|
+
return _context4.f(8);
|
|
743
|
+
case 9:
|
|
744
|
+
_context4.n = 12;
|
|
745
|
+
break;
|
|
746
|
+
case 10:
|
|
747
|
+
_context4.p = 10;
|
|
748
|
+
_t = _context4.v;
|
|
749
|
+
if (isAbort(_t)) {
|
|
750
|
+
_context4.n = 11;
|
|
751
|
+
break;
|
|
752
|
+
}
|
|
753
|
+
throw _t;
|
|
754
|
+
case 11:
|
|
755
|
+
aborted = true;
|
|
756
|
+
discard(wasm);
|
|
757
|
+
throw outOfMemory();
|
|
758
|
+
case 12:
|
|
759
|
+
_context4.p = 12;
|
|
760
|
+
if (!aborted) {
|
|
761
|
+
if (job) {
|
|
762
|
+
wasm._clustering_job_free(job);
|
|
763
|
+
}
|
|
764
|
+
wasm._free(imagePointer);
|
|
765
|
+
}
|
|
766
|
+
return _context4.f(12);
|
|
767
|
+
case 13:
|
|
768
|
+
return _context4.a(2);
|
|
769
|
+
}
|
|
770
|
+
}, _callee4, null, [[7,, 8, 9], [2, 10, 12, 13]]);
|
|
771
|
+
}));
|
|
772
|
+
return function clusterInSlices(_x3, _x4, _x5, _x6, _x7) {
|
|
773
|
+
return _ref5.apply(this, arguments);
|
|
774
|
+
};
|
|
775
|
+
}();
|
|
230
776
|
var getClustering = exports.getClustering = /*#__PURE__*/function () {
|
|
231
|
-
var
|
|
232
|
-
var o;
|
|
233
|
-
return _regenerator().w(function (
|
|
234
|
-
while (1) switch (
|
|
777
|
+
var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(image, options) {
|
|
778
|
+
var wasm, o, report, clustering;
|
|
779
|
+
return _regenerator().w(function (_context5) {
|
|
780
|
+
while (1) switch (_context5.n) {
|
|
235
781
|
case 0:
|
|
236
|
-
|
|
237
|
-
return
|
|
782
|
+
_context5.n = 1;
|
|
783
|
+
return acquire();
|
|
238
784
|
case 1:
|
|
785
|
+
wasm = _context5.v;
|
|
239
786
|
image = load(image);
|
|
240
787
|
o = parseOptions(options, clusterOptions, image);
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
788
|
+
if (!isControlled(o)) {
|
|
789
|
+
_context5.n = 3;
|
|
790
|
+
break;
|
|
791
|
+
}
|
|
792
|
+
report = reporter(o);
|
|
793
|
+
_context5.n = 2;
|
|
794
|
+
return clusterInSlices(wasm, image, o, report, 1);
|
|
795
|
+
case 2:
|
|
796
|
+
clustering = _context5.v;
|
|
797
|
+
report(1);
|
|
798
|
+
return _context5.a(2, (0, _shared.markClustering)(clustering));
|
|
799
|
+
case 3:
|
|
800
|
+
return _context5.a(2, (0, _shared.markClustering)(call(wasm, [image.data], function (imagePointer) {
|
|
801
|
+
return wasm._get_clustering(imagePointer, image.data.length, codes[image.format], o.colorSpace, o.precision, o.alpha, o.transparent);
|
|
802
|
+
})));
|
|
244
803
|
}
|
|
245
|
-
},
|
|
804
|
+
}, _callee5);
|
|
246
805
|
}));
|
|
247
|
-
return function getClustering(
|
|
248
|
-
return
|
|
806
|
+
return function getClustering(_x8, _x9) {
|
|
807
|
+
return _ref6.apply(this, arguments);
|
|
249
808
|
};
|
|
250
809
|
}();
|
|
251
810
|
var getPalette = exports.getPalette = /*#__PURE__*/function () {
|
|
252
|
-
var
|
|
253
|
-
var o;
|
|
254
|
-
return _regenerator().w(function (
|
|
255
|
-
while (1) switch (
|
|
811
|
+
var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(image, k, options) {
|
|
812
|
+
var wasm, o, report, clustering, palette;
|
|
813
|
+
return _regenerator().w(function (_context6) {
|
|
814
|
+
while (1) switch (_context6.n) {
|
|
256
815
|
case 0:
|
|
257
|
-
|
|
258
|
-
return
|
|
816
|
+
_context6.n = 1;
|
|
817
|
+
return acquire();
|
|
259
818
|
case 1:
|
|
819
|
+
wasm = _context6.v;
|
|
260
820
|
image = load(image);
|
|
261
|
-
check(k);
|
|
821
|
+
k = check(k);
|
|
262
822
|
o = parseOptions(options, clusterOptions, image);
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
823
|
+
if (!isControlled(o)) {
|
|
824
|
+
_context6.n = 3;
|
|
825
|
+
break;
|
|
826
|
+
}
|
|
827
|
+
report = reporter(o);
|
|
828
|
+
_context6.n = 2;
|
|
829
|
+
return clusterInSlices(wasm, image, o, report, 0.99);
|
|
830
|
+
case 2:
|
|
831
|
+
clustering = _context6.v;
|
|
832
|
+
checkAborted(o.signal);
|
|
833
|
+
palette = call(wasm, [clustering], function (clusteringPointer) {
|
|
834
|
+
return wasm._get_palette_from_clustering(clusteringPointer, clustering.length, k);
|
|
835
|
+
});
|
|
836
|
+
report(1);
|
|
837
|
+
return _context6.a(2, (0, _shared.markPalette)(palette, (0, _shared.colorSpaceName)(o.colorSpace)));
|
|
838
|
+
case 3:
|
|
839
|
+
return _context6.a(2, (0, _shared.markPalette)(call(wasm, [image.data], function (imagePointer) {
|
|
840
|
+
return wasm._get_palette(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.transparent);
|
|
841
|
+
}), (0, _shared.colorSpaceName)(o.colorSpace)));
|
|
266
842
|
}
|
|
267
|
-
},
|
|
843
|
+
}, _callee6);
|
|
268
844
|
}));
|
|
269
|
-
return function getPalette(
|
|
270
|
-
return
|
|
845
|
+
return function getPalette(_x0, _x1, _x10) {
|
|
846
|
+
return _ref7.apply(this, arguments);
|
|
271
847
|
};
|
|
272
848
|
}();
|
|
273
849
|
var getPaletteFromClustering = exports.getPaletteFromClustering = /*#__PURE__*/function () {
|
|
274
|
-
var
|
|
275
|
-
|
|
276
|
-
|
|
850
|
+
var _ref8 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(clustering, k) {
|
|
851
|
+
var wasm, header;
|
|
852
|
+
return _regenerator().w(function (_context7) {
|
|
853
|
+
while (1) switch (_context7.n) {
|
|
277
854
|
case 0:
|
|
278
|
-
|
|
279
|
-
return
|
|
855
|
+
_context7.n = 1;
|
|
856
|
+
return acquire();
|
|
280
857
|
case 1:
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
858
|
+
wasm = _context7.v;
|
|
859
|
+
header = (0, _shared.readHeader)(clustering);
|
|
860
|
+
k = check(k);
|
|
861
|
+
return _context7.a(2, (0, _shared.markPalette)(call(wasm, [clustering], function (clusteringPointer) {
|
|
862
|
+
return wasm._get_palette_from_clustering(clusteringPointer, clustering.length, k);
|
|
863
|
+
}), (0, _shared.colorSpaceName)(header.colorSpace)));
|
|
286
864
|
}
|
|
287
|
-
},
|
|
865
|
+
}, _callee7);
|
|
288
866
|
}));
|
|
289
|
-
return function getPaletteFromClustering(
|
|
290
|
-
return
|
|
867
|
+
return function getPaletteFromClustering(_x11, _x12) {
|
|
868
|
+
return _ref8.apply(this, arguments);
|
|
869
|
+
};
|
|
870
|
+
}();
|
|
871
|
+
|
|
872
|
+
// The weight of each colour in the k-colour cut, in pixels (opacity-weighted), read from the clustering's records.
|
|
873
|
+
var weightsOf = function weightsOf(wasm, clustering, header, k) {
|
|
874
|
+
var bytes = call(wasm, [clustering], function (clusteringPointer) {
|
|
875
|
+
return wasm._get_palette_weights(clusteringPointer, clustering.length, k);
|
|
876
|
+
});
|
|
877
|
+
if (header.merges > 0 && bytes.length === 0) {
|
|
878
|
+
throw (0, _shared.fail)('invalid-clustering', "Invalid clustering: its weights are inconsistent.", 'clustering');
|
|
879
|
+
}
|
|
880
|
+
return new Float64Array(bytes.buffer, bytes.byteOffset, bytes.length / 8);
|
|
881
|
+
};
|
|
882
|
+
|
|
883
|
+
// A palette with the weight and share of the image of each of its colours, from one cut of one clustering, so the three
|
|
884
|
+
// arrays line up by construction. Weights are pixels: fully transparent pixels count 0 and partly transparent ones in
|
|
885
|
+
// proportion to their opacity, as in clustering. Shares are the weights divided by their total, so they sum to 1.
|
|
886
|
+
var getSwatchesFromClustering = exports.getSwatchesFromClustering = /*#__PURE__*/function () {
|
|
887
|
+
var _ref9 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(clustering, k) {
|
|
888
|
+
var wasm, header, palette, weights, total, _iterator3, _step3, weight, shares, i;
|
|
889
|
+
return _regenerator().w(function (_context8) {
|
|
890
|
+
while (1) switch (_context8.n) {
|
|
891
|
+
case 0:
|
|
892
|
+
_context8.n = 1;
|
|
893
|
+
return acquire();
|
|
894
|
+
case 1:
|
|
895
|
+
wasm = _context8.v;
|
|
896
|
+
header = (0, _shared.readHeader)(clustering);
|
|
897
|
+
k = check(k);
|
|
898
|
+
if (!(header.version < 3)) {
|
|
899
|
+
_context8.n = 2;
|
|
900
|
+
break;
|
|
901
|
+
}
|
|
902
|
+
throw (0, _shared.fail)('unsupported-version', "Invalid clustering: format version ".concat(header.version, " (agglomerative-clustering ").concat(header.version, ".x) has no weights. Cluster the image again."), 'clustering');
|
|
903
|
+
case 2:
|
|
904
|
+
palette = (0, _shared.markPalette)(call(wasm, [clustering], function (clusteringPointer) {
|
|
905
|
+
return wasm._get_palette_from_clustering(clusteringPointer, clustering.length, k);
|
|
906
|
+
}), (0, _shared.colorSpaceName)(header.colorSpace));
|
|
907
|
+
weights = weightsOf(wasm, clustering, header, k);
|
|
908
|
+
total = 0;
|
|
909
|
+
_iterator3 = _createForOfIteratorHelper(weights);
|
|
910
|
+
try {
|
|
911
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
912
|
+
weight = _step3.value;
|
|
913
|
+
total += weight;
|
|
914
|
+
}
|
|
915
|
+
} catch (err) {
|
|
916
|
+
_iterator3.e(err);
|
|
917
|
+
} finally {
|
|
918
|
+
_iterator3.f();
|
|
919
|
+
}
|
|
920
|
+
shares = new Float64Array(weights.length);
|
|
921
|
+
if (total > 0) {
|
|
922
|
+
for (i = 0; i < weights.length; i++) {
|
|
923
|
+
shares[i] = weights[i] / total;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
return _context8.a(2, {
|
|
927
|
+
palette: palette,
|
|
928
|
+
weights: weights,
|
|
929
|
+
shares: shares
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
}, _callee8);
|
|
933
|
+
}));
|
|
934
|
+
return function getSwatchesFromClustering(_x13, _x14) {
|
|
935
|
+
return _ref9.apply(this, arguments);
|
|
936
|
+
};
|
|
937
|
+
}();
|
|
938
|
+
|
|
939
|
+
// The same for an image: clusters it, then cuts, like getPalette.
|
|
940
|
+
var getSwatches = exports.getSwatches = /*#__PURE__*/function () {
|
|
941
|
+
var _ref0 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(image, k, options) {
|
|
942
|
+
var _t2, _t3, _t4;
|
|
943
|
+
return _regenerator().w(function (_context9) {
|
|
944
|
+
while (1) switch (_context9.n) {
|
|
945
|
+
case 0:
|
|
946
|
+
// Arguments are checked in the order they are given (image, then k, then options), as in every other function.
|
|
947
|
+
load(image);
|
|
948
|
+
k = check(k);
|
|
949
|
+
_t2 = getSwatchesFromClustering;
|
|
950
|
+
_context9.n = 1;
|
|
951
|
+
return getClustering(image, options);
|
|
952
|
+
case 1:
|
|
953
|
+
_t3 = _context9.v;
|
|
954
|
+
_t4 = k;
|
|
955
|
+
return _context9.a(2, _t2(_t3, _t4));
|
|
956
|
+
}
|
|
957
|
+
}, _callee9);
|
|
958
|
+
}));
|
|
959
|
+
return function getSwatches(_x15, _x16, _x17) {
|
|
960
|
+
return _ref0.apply(this, arguments);
|
|
961
|
+
};
|
|
962
|
+
}();
|
|
963
|
+
|
|
964
|
+
// What a clustering holds, read from its header and records without the WASM module. `mse[k - 1]` is the mean
|
|
965
|
+
// squared error per channel of the k-colour clustering: each pixel's squared distance to its cluster's colour,
|
|
966
|
+
// weighted as in clustering, in 8-bit RGB values (RGB) or OKLab units (OKLab).
|
|
967
|
+
var getClusteringInfo = exports.getClusteringInfo = /*#__PURE__*/function () {
|
|
968
|
+
var _ref1 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(clustering) {
|
|
969
|
+
var header, colors, info, view, pixels, total, remaining, i, _i4;
|
|
970
|
+
return _regenerator().w(function (_context0) {
|
|
971
|
+
while (1) switch (_context0.n) {
|
|
972
|
+
case 0:
|
|
973
|
+
header = (0, _shared.readHeader)(clustering);
|
|
974
|
+
colors = header.merges + (clustering.length >= header.headerLength + 3 ? 1 : 0);
|
|
975
|
+
info = {
|
|
976
|
+
formatVersion: header.version,
|
|
977
|
+
colorSpace: (0, _shared.colorSpaceName)(header.colorSpace),
|
|
978
|
+
precision: header.precision,
|
|
979
|
+
transparent: header.transparent === 0 ? false : header.transparent,
|
|
980
|
+
maxColors: colors,
|
|
981
|
+
totalWeight: null,
|
|
982
|
+
mse: null,
|
|
983
|
+
explained: null
|
|
984
|
+
};
|
|
985
|
+
if (!(header.version < 3)) {
|
|
986
|
+
_context0.n = 1;
|
|
987
|
+
break;
|
|
988
|
+
}
|
|
989
|
+
return _context0.a(2, info);
|
|
990
|
+
case 1:
|
|
991
|
+
view = new DataView(clustering.buffer, clustering.byteOffset, clustering.byteLength);
|
|
992
|
+
pixels = Math.pow(2, clustering[7]) / (clustering[6] === 1 ? 255 : 1);
|
|
993
|
+
total = view.getUint32(8, true);
|
|
994
|
+
info.totalWeight = total * pixels;
|
|
995
|
+
info.mse = new Float64Array(colors);
|
|
996
|
+
info.explained = new Float64Array(colors);
|
|
997
|
+
// The error of the leaf clusters: what `precision` below 8 discarded by grouping colours (0 before it was recorded).
|
|
998
|
+
remaining = header.headerLength >= 17 ? view.getFloat32(13, true) : 0;
|
|
999
|
+
if (colors > 0) {
|
|
1000
|
+
info.mse[colors - 1] = remaining / total / 3;
|
|
1001
|
+
}
|
|
1002
|
+
for (i = header.merges - 1; i >= 0; i--) {
|
|
1003
|
+
remaining += view.getFloat32(header.headerLength + 3 + i * header.recordLength + 17, true);
|
|
1004
|
+
info.mse[i] = remaining / total / 3;
|
|
1005
|
+
}
|
|
1006
|
+
// The share of the image's variance each palette size keeps; 1 throughout for an image of one colour.
|
|
1007
|
+
for (_i4 = 0; _i4 < colors; _i4++) {
|
|
1008
|
+
info.explained[_i4] = info.mse[0] > 0 ? Math.max(0, Math.min(1, 1 - info.mse[_i4] / info.mse[0])) : 1;
|
|
1009
|
+
}
|
|
1010
|
+
return _context0.a(2, info);
|
|
1011
|
+
}
|
|
1012
|
+
}, _callee0);
|
|
1013
|
+
}));
|
|
1014
|
+
return function getClusteringInfo(_x18) {
|
|
1015
|
+
return _ref1.apply(this, arguments);
|
|
291
1016
|
};
|
|
292
1017
|
}();
|
|
293
1018
|
var quantize = exports.quantize = /*#__PURE__*/function () {
|
|
294
|
-
var
|
|
295
|
-
var wrap, o;
|
|
296
|
-
return _regenerator().w(function (
|
|
297
|
-
while (1) switch (
|
|
1019
|
+
var _ref10 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1(image, k, options) {
|
|
1020
|
+
var wasm, wrap, o, report, clustering, mapped;
|
|
1021
|
+
return _regenerator().w(function (_context1) {
|
|
1022
|
+
while (1) switch (_context1.n) {
|
|
298
1023
|
case 0:
|
|
299
|
-
|
|
300
|
-
return
|
|
1024
|
+
_context1.n = 1;
|
|
1025
|
+
return acquire();
|
|
301
1026
|
case 1:
|
|
1027
|
+
wasm = _context1.v;
|
|
302
1028
|
wrap = getWrapper(image);
|
|
303
1029
|
image = load(image);
|
|
304
|
-
check(k);
|
|
1030
|
+
k = check(k);
|
|
305
1031
|
o = parseOptions(options, quantizeOptions, image);
|
|
306
|
-
|
|
307
|
-
|
|
1032
|
+
if (!isControlled(o)) {
|
|
1033
|
+
_context1.n = 3;
|
|
1034
|
+
break;
|
|
1035
|
+
}
|
|
1036
|
+
report = reporter(o);
|
|
1037
|
+
_context1.n = 2;
|
|
1038
|
+
return clusterInSlices(wasm, image, o, report, 0.9);
|
|
1039
|
+
case 2:
|
|
1040
|
+
clustering = _context1.v;
|
|
1041
|
+
checkAborted(o.signal);
|
|
1042
|
+
mapped = wrap(call(wasm, [image.data, clustering], function (imagePointer, clusteringPointer) {
|
|
1043
|
+
return wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.width);
|
|
1044
|
+
}));
|
|
1045
|
+
report(1);
|
|
1046
|
+
return _context1.a(2, mapped);
|
|
1047
|
+
case 3:
|
|
1048
|
+
return _context1.a(2, wrap(call(wasm, [image.data], function (imagePointer) {
|
|
1049
|
+
return wasm._quantize(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.dither, o.width, o.transparent);
|
|
308
1050
|
})));
|
|
309
1051
|
}
|
|
310
|
-
},
|
|
1052
|
+
}, _callee1);
|
|
311
1053
|
}));
|
|
312
|
-
return function quantize(
|
|
313
|
-
return
|
|
1054
|
+
return function quantize(_x19, _x20, _x21) {
|
|
1055
|
+
return _ref10.apply(this, arguments);
|
|
314
1056
|
};
|
|
315
1057
|
}();
|
|
316
|
-
var
|
|
317
|
-
var
|
|
318
|
-
var wrap, o, colorSpace;
|
|
319
|
-
return _regenerator().w(function (
|
|
320
|
-
while (1) switch (
|
|
1058
|
+
var quantizeFromClustering = exports.quantizeFromClustering = /*#__PURE__*/function () {
|
|
1059
|
+
var _ref11 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10(image, clustering, k, options) {
|
|
1060
|
+
var wasm, wrap, o, colorSpace, result;
|
|
1061
|
+
return _regenerator().w(function (_context10) {
|
|
1062
|
+
while (1) switch (_context10.n) {
|
|
321
1063
|
case 0:
|
|
322
|
-
|
|
323
|
-
return
|
|
1064
|
+
_context10.n = 1;
|
|
1065
|
+
return acquire();
|
|
324
1066
|
case 1:
|
|
1067
|
+
wasm = _context10.v;
|
|
325
1068
|
wrap = getWrapper(image);
|
|
326
1069
|
image = load(image);
|
|
327
|
-
|
|
328
|
-
check(k);
|
|
1070
|
+
(0, _shared.readHeader)(clustering);
|
|
1071
|
+
k = check(k);
|
|
329
1072
|
o = parseOptions(options, mapOptions, image);
|
|
1073
|
+
checkAborted(o.signal);
|
|
330
1074
|
colorSpace = o.colorSpaceGiven ? o.colorSpace : -1;
|
|
331
|
-
|
|
332
|
-
return
|
|
333
|
-
}))
|
|
1075
|
+
result = wrap(call(wasm, [image.data, clustering], function (imagePointer, clusteringPointer) {
|
|
1076
|
+
return wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.width);
|
|
1077
|
+
}));
|
|
1078
|
+
return _context10.a(2, result);
|
|
334
1079
|
}
|
|
335
|
-
},
|
|
1080
|
+
}, _callee10);
|
|
336
1081
|
}));
|
|
337
|
-
return function
|
|
338
|
-
return
|
|
1082
|
+
return function quantizeFromClustering(_x22, _x23, _x24, _x25) {
|
|
1083
|
+
return _ref11.apply(this, arguments);
|
|
339
1084
|
};
|
|
340
1085
|
}();
|
|
341
1086
|
var quantizeWithPalette = exports.quantizeWithPalette = /*#__PURE__*/function () {
|
|
342
|
-
var
|
|
343
|
-
var wrap, o;
|
|
344
|
-
return _regenerator().w(function (
|
|
345
|
-
while (1) switch (
|
|
1087
|
+
var _ref12 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11(image, palette, options) {
|
|
1088
|
+
var wasm, wrap, o, result;
|
|
1089
|
+
return _regenerator().w(function (_context11) {
|
|
1090
|
+
while (1) switch (_context11.n) {
|
|
346
1091
|
case 0:
|
|
347
|
-
|
|
348
|
-
return
|
|
1092
|
+
_context11.n = 1;
|
|
1093
|
+
return acquire();
|
|
349
1094
|
case 1:
|
|
1095
|
+
wasm = _context11.v;
|
|
350
1096
|
wrap = getWrapper(image);
|
|
351
1097
|
image = load(image);
|
|
1098
|
+
(0, _shared.checkPalette)(palette);
|
|
352
1099
|
o = parseOptions(options, mapOptions, image);
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
1100
|
+
checkAborted(o.signal);
|
|
1101
|
+
result = wrap(call(wasm, [image.data, palette], function (imagePointer, palettePointer) {
|
|
1102
|
+
return wasm._quantize_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, matchingSpace(o, palette), o.dither, o.width);
|
|
1103
|
+
}));
|
|
1104
|
+
return _context11.a(2, result);
|
|
356
1105
|
}
|
|
357
|
-
},
|
|
1106
|
+
}, _callee11);
|
|
358
1107
|
}));
|
|
359
|
-
return function quantizeWithPalette(
|
|
360
|
-
return
|
|
1108
|
+
return function quantizeWithPalette(_x26, _x27, _x28) {
|
|
1109
|
+
return _ref12.apply(this, arguments);
|
|
1110
|
+
};
|
|
1111
|
+
}();
|
|
1112
|
+
var quantizeToIndices = exports.quantizeToIndices = /*#__PURE__*/function () {
|
|
1113
|
+
var _ref13 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee12(image, k, options) {
|
|
1114
|
+
var wasm, original, o, report, clustering, indexed;
|
|
1115
|
+
return _regenerator().w(function (_context12) {
|
|
1116
|
+
while (1) switch (_context12.n) {
|
|
1117
|
+
case 0:
|
|
1118
|
+
_context12.n = 1;
|
|
1119
|
+
return acquire();
|
|
1120
|
+
case 1:
|
|
1121
|
+
wasm = _context12.v;
|
|
1122
|
+
original = image;
|
|
1123
|
+
image = load(image);
|
|
1124
|
+
k = check(k);
|
|
1125
|
+
o = parseOptions(options, quantizeIndexOptions, image);
|
|
1126
|
+
if (!isControlled(o)) {
|
|
1127
|
+
_context12.n = 3;
|
|
1128
|
+
break;
|
|
1129
|
+
}
|
|
1130
|
+
report = reporter(o);
|
|
1131
|
+
_context12.n = 2;
|
|
1132
|
+
return clusterInSlices(wasm, image, o, report, 0.9);
|
|
1133
|
+
case 2:
|
|
1134
|
+
clustering = _context12.v;
|
|
1135
|
+
checkAborted(o.signal);
|
|
1136
|
+
indexed = unpackIndices(call(wasm, [image.data, clustering], function (imagePointer, clusteringPointer) {
|
|
1137
|
+
return wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.width, o.transparent);
|
|
1138
|
+
}), original, (0, _shared.colorSpaceName)(o.colorSpace));
|
|
1139
|
+
report(1);
|
|
1140
|
+
return _context12.a(2, indexed);
|
|
1141
|
+
case 3:
|
|
1142
|
+
return _context12.a(2, unpackIndices(call(wasm, [image.data], function (imagePointer) {
|
|
1143
|
+
return wasm._quantize_indices(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.dither, o.width, o.transparent);
|
|
1144
|
+
}), original, (0, _shared.colorSpaceName)(o.colorSpace)));
|
|
1145
|
+
}
|
|
1146
|
+
}, _callee12);
|
|
1147
|
+
}));
|
|
1148
|
+
return function quantizeToIndices(_x29, _x30, _x31) {
|
|
1149
|
+
return _ref13.apply(this, arguments);
|
|
1150
|
+
};
|
|
1151
|
+
}();
|
|
1152
|
+
var quantizeToIndicesFromClustering = exports.quantizeToIndicesFromClustering = /*#__PURE__*/function () {
|
|
1153
|
+
var _ref14 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee13(image, clustering, k, options) {
|
|
1154
|
+
var wasm, original, header, o, colorSpace, cutoff, result;
|
|
1155
|
+
return _regenerator().w(function (_context13) {
|
|
1156
|
+
while (1) switch (_context13.n) {
|
|
1157
|
+
case 0:
|
|
1158
|
+
_context13.n = 1;
|
|
1159
|
+
return acquire();
|
|
1160
|
+
case 1:
|
|
1161
|
+
wasm = _context13.v;
|
|
1162
|
+
original = image;
|
|
1163
|
+
image = load(image);
|
|
1164
|
+
header = (0, _shared.readHeader)(clustering);
|
|
1165
|
+
k = check(k);
|
|
1166
|
+
o = parseOptions(options, mapIndexOptions, image);
|
|
1167
|
+
checkAborted(o.signal);
|
|
1168
|
+
colorSpace = o.colorSpaceGiven ? o.colorSpace : -1; // The cutoff the clustering was made with is the default, so clustering and mapping agree unless a cutoff is named.
|
|
1169
|
+
cutoff = o.transparentGiven ? o.transparent : header.transparent;
|
|
1170
|
+
result = unpackIndices(call(wasm, [image.data, clustering], function (imagePointer, clusteringPointer) {
|
|
1171
|
+
return wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.width, cutoff);
|
|
1172
|
+
}), original, (0, _shared.colorSpaceName)(header.colorSpace));
|
|
1173
|
+
return _context13.a(2, result);
|
|
1174
|
+
}
|
|
1175
|
+
}, _callee13);
|
|
1176
|
+
}));
|
|
1177
|
+
return function quantizeToIndicesFromClustering(_x32, _x33, _x34, _x35) {
|
|
1178
|
+
return _ref14.apply(this, arguments);
|
|
1179
|
+
};
|
|
1180
|
+
}();
|
|
1181
|
+
var quantizeToIndicesWithPalette = exports.quantizeToIndicesWithPalette = /*#__PURE__*/function () {
|
|
1182
|
+
var _ref15 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee14(image, palette, options) {
|
|
1183
|
+
var wasm, original, o, space, result;
|
|
1184
|
+
return _regenerator().w(function (_context14) {
|
|
1185
|
+
while (1) switch (_context14.n) {
|
|
1186
|
+
case 0:
|
|
1187
|
+
_context14.n = 1;
|
|
1188
|
+
return acquire();
|
|
1189
|
+
case 1:
|
|
1190
|
+
wasm = _context14.v;
|
|
1191
|
+
original = image;
|
|
1192
|
+
image = load(image);
|
|
1193
|
+
(0, _shared.checkPalette)(palette);
|
|
1194
|
+
o = parseOptions(options, mapIndexOptions, image);
|
|
1195
|
+
checkAborted(o.signal);
|
|
1196
|
+
space = matchingSpace(o, palette);
|
|
1197
|
+
result = unpackIndices(call(wasm, [image.data, palette], function (imagePointer, palettePointer) {
|
|
1198
|
+
return wasm._quantize_indices_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, space, o.dither, o.width, o.transparent);
|
|
1199
|
+
}), original, (0, _shared.isPalette)(palette) ? palette.colorSpace : (0, _shared.colorSpaceName)(space));
|
|
1200
|
+
return _context14.a(2, result);
|
|
1201
|
+
}
|
|
1202
|
+
}, _callee14);
|
|
1203
|
+
}));
|
|
1204
|
+
return function quantizeToIndicesWithPalette(_x36, _x37, _x38) {
|
|
1205
|
+
return _ref15.apply(this, arguments);
|
|
361
1206
|
};
|
|
362
1207
|
}();
|