agglomerative-clustering 3.0.0-beta.1 → 3.0.0-beta.2

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/dist/index.cjs CHANGED
@@ -26,9 +26,9 @@ var _clustering = _interopRequireDefault(require("./clustering.js"));
26
26
  var _shared = require("./shared.cjs");
27
27
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
28
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; } } }; }
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); }
30
29
  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 }; })(); }
31
30
  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); }
31
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t15 in e) "default" !== _t15 && {}.hasOwnProperty.call(e, _t15) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t15)) && (i.get || i.set) ? o(f, _t15, i) : f[_t15] = e[_t15]); return f; })(e, t); }
32
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); }
33
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
34
  function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
@@ -36,10 +36,28 @@ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread n
36
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
37
  function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
38
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).
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; }
40
+ 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); } // The library's version, the stored-data format version, and the Palette and Clustering types (see shared.js).
40
41
  // The version of stored data (clusterings) this build writes. It reads this one and format 2 (see readHeader). Unlike
41
42
  // `version`, it changes only when the stored format does, so a 3.x release that keeps the format keeps this number.
42
43
  var formatVersion = exports.formatVersion = _shared.latestVersion;
44
+
45
+ // Checked by tag rather than `instanceof`, so values from another realm are recognised: an ImageData from an iframe's
46
+ // canvas, a WebAssembly.Module compiled by a bundler's runtime (Next.js Edge's `import wasm from '…wasm?module'`), a URL
47
+ // from a vm context. A tag can be faked, but only on purpose, and the value is then used as what it claims to be.
48
+ var tagOf = function tagOf(value) {
49
+ return Object.prototype.toString.call(value);
50
+ };
51
+ // (`instanceof` is kept too, for implementations without the tag, such as node-canvas's ImageData.)
52
+ var isImageData = function isImageData(value) {
53
+ return _typeof(value) === 'object' && value !== null && (typeof ImageData !== 'undefined' && value instanceof ImageData || tagOf(value) === '[object ImageData]');
54
+ };
55
+ var isWasmModule = function isWasmModule(value) {
56
+ return _typeof(value) === 'object' && value !== null && (typeof WebAssembly !== 'undefined' && value instanceof WebAssembly.Module || tagOf(value) === '[object WebAssembly.Module]');
57
+ };
58
+ var isURL = function isURL(value) {
59
+ return _typeof(value) === 'object' && value !== null && (typeof URL !== 'undefined' && value instanceof URL || tagOf(value) === '[object URL]');
60
+ };
43
61
  var codes = {
44
62
  'rgba': 0,
45
63
  'rgb': 1
@@ -58,7 +76,7 @@ var wasmSettings = function wasmSettings(wasm, onError) {
58
76
  if (wasm === undefined) {
59
77
  return {};
60
78
  }
61
- if (typeof wasm === 'string' || wasm instanceof URL) {
79
+ if (typeof wasm === 'string' || isURL(wasm)) {
62
80
  var location = String(wasm);
63
81
  return {
64
82
  locateFile: function locateFile(path, directory) {
@@ -66,22 +84,35 @@ var wasmSettings = function wasmSettings(wasm, onError) {
66
84
  }
67
85
  };
68
86
  }
69
- // Bytes and compiled modules go through `instantiateWasm`: this build of the glue reads no `wasmBinary` setting.
87
+ // Bytes and compiled modules go through `instantiateWasm`: this build of the glue reads no `wasmBinary` setting. The glue
88
+ // sets itself up on the instance inside `receive`, which throws for a module that isn't this one (it has other exports);
89
+ // that is reported as a failed load rather than left to escape, which would leave the load waiting forever.
90
+ var accept = function accept(receive) {
91
+ return function (instance) {
92
+ try {
93
+ receive(instance);
94
+ } catch (error) {
95
+ onError(Object.assign(new Error("it is not agglomerative-clustering's module (it lacks the memory and functions this script uses)"), {
96
+ cause: error
97
+ }));
98
+ }
99
+ };
100
+ };
70
101
  if ((0, _shared.isArrayBufferLike)(wasm) || ArrayBuffer.isView(wasm)) {
71
102
  var source = (0, _shared.isArrayBufferLike)(wasm) ? wasm : new Uint8Array(wasm.buffer, wasm.byteOffset, wasm.byteLength);
72
103
  return {
73
104
  instantiateWasm: function instantiateWasm(imports, receive) {
74
105
  WebAssembly.instantiate(source, imports).then(function (_ref) {
75
106
  var instance = _ref.instance;
76
- return receive(instance);
107
+ return accept(receive)(instance);
77
108
  }, onError);
78
109
  }
79
110
  };
80
111
  }
81
- if (typeof WebAssembly !== 'undefined' && wasm instanceof WebAssembly.Module) {
112
+ if (isWasmModule(wasm)) {
82
113
  return {
83
114
  instantiateWasm: function instantiateWasm(imports, receive) {
84
- WebAssembly.instantiate(wasm, imports).then(receive, onError);
115
+ WebAssembly.instantiate(wasm, imports).then(accept(receive), onError);
85
116
  }
86
117
  };
87
118
  }
@@ -89,9 +120,38 @@ var wasmSettings = function wasmSettings(wasm, onError) {
89
120
  };
90
121
 
91
122
  // 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 }).")), {
123
+ // bundler that moved the file, an engine without WebAssembly SIMD. The engine's own error (a CompileError, a failed fetch)
124
+ // says little about which, so the message leads with what happened and then gives the likely fix for where it ran.
125
+ // The smallest module that uses a SIMD instruction (from wasm-feature-detect), to tell an old engine from a missing file.
126
+ var simdProbe = [0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 10, 1, 8, 0, 65, 0, 253, 15, 253, 98, 11];
127
+ var hasSimd = function hasSimd() {
128
+ try {
129
+ return (typeof WebAssembly === "undefined" ? "undefined" : _typeof(WebAssembly)) === 'object' && WebAssembly.validate(new Uint8Array(simdProbe));
130
+ } catch (error) {
131
+ return false;
132
+ }
133
+ };
134
+ var isNodeLike = typeof process !== 'undefined' && !!(process.versions && process.versions.node);
135
+ var wasmLoadFailed = function wasmLoadFailed(cause, given) {
136
+ var text = String(cause && cause.message !== undefined ? cause.message : cause).split('\n')[0];
137
+ // Emscripten appends advice about its own debug build, which doesn't apply to this package's users.
138
+ // and wraps a failed compile in Aborted(...), which is how it stops itself, not part of the reason.
139
+ var reason = text.replace(/\s*\(?Build with -sASSERTIONS for more info\.?\)?/, '').trim().replace(/\.+$/, '');
140
+ var aborted = /^Aborted\((.*)\)$/.exec(reason);
141
+ if (aborted) {
142
+ reason = aborted[1].replace(/\.+$/, '');
143
+ }
144
+ var advice;
145
+ if (!hasSimd()) {
146
+ advice = "This engine has no WebAssembly SIMD, which the module needs (Chrome and Edge 91, Firefox 89, Safari 16.4, Node 16.4 or newer).";
147
+ } else if (given) {
148
+ advice = "Check what init({ wasm }) was given: it must be this package's clustering.wasm (its URL or path, its bytes, or the module compiled from it).";
149
+ } else if (isNodeLike) {
150
+ advice = "If the package was bundled into a server build (Next.js and others), the bundler may have moved or renamed clustering.wasm: leave the package out of the bundle (in Next.js, serverExternalPackages: ['agglomerative-clustering']) or give the module with init({ wasm }).";
151
+ } else {
152
+ advice = "Check that clustering.wasm is served next to the script (not a 404, and with the application/wasm type) and allowed by the page's Content-Security-Policy, or say where it is with init({ wasm }).";
153
+ }
154
+ return Object.assign((0, _shared.fail)('wasm-load-failed', "Could not load the WebAssembly module: ".concat(reason || 'unknown error', ". ").concat(advice)), {
95
155
  cause: cause
96
156
  });
97
157
  };
@@ -101,38 +161,93 @@ var wasmLoadFailed = function wasmLoadFailed(cause) {
101
161
  // pointing at the wrong one) would misbehave in ways nothing explains, so the module says which version it is and a
102
162
  // mismatch is reported when it loads.
103
163
  var checkWasmVersion = function checkWasmVersion(instance) {
104
- var found = 'an unknown version (from before 3.0)';
105
- if (typeof instance._library_version === 'function') {
164
+ // A module from before 3.0 has no version export (the glue's wrapper then throws or returns nothing), and one that isn't
165
+ // this library's at all may have none either: both are reported as a version mismatch, never as a stray TypeError.
166
+ var found = '';
167
+ try {
168
+ var pointer = instance._library_version();
106
169
  var heap = instance.HEAPU8;
107
- var end = instance._library_version();
108
- var start = end;
109
- while (heap[end] !== 0 && end - start < 64) {
110
- end++;
170
+ if (typeof pointer === 'number' && pointer > 0 && heap) {
171
+ var end = pointer;
172
+ while (heap[end] !== 0 && end - pointer < 64) {
173
+ end++;
174
+ }
175
+ found = String.fromCharCode.apply(String, _toConsumableArray(heap.subarray(pointer, end)));
111
176
  }
112
- found = String.fromCharCode.apply(String, _toConsumableArray(heap.subarray(start, end)));
113
- }
177
+ } catch (error) {/* no version export */}
114
178
  var expected = "agglomerative-clustering@".concat(_shared.version);
115
179
  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."));
180
+ var what = found ? found : 'from another version (one from before 3.0, which does not say which, or not this library\'s module at all)';
181
+ throw (0, _shared.fail)('wasm-load-failed', "The WebAssembly module is ".concat(what, ", 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
182
  }
118
183
  };
119
184
 
185
+ // Instantiates the module from init's `wasm` option (undefined: the glue finds the .wasm next to itself).
186
+ var instantiate = function instantiate(wasm) {
187
+ var reject;
188
+ var failed = new Promise(function (_, r) {
189
+ reject = r;
190
+ });
191
+ failed["catch"](function () {});
192
+ return Promise.race([(0, _clustering["default"])(wasmSettings(wasm, reject)), failed]);
193
+ };
194
+
195
+ // The bytes of this package's clustering.wasm as Node resolves it from the working directory, for when the copy next to
196
+ // the script is missing (see acquire). The specifiers are built at run time so bundlers leave them alone (see worker.js).
197
+ var packagedWasm = /*#__PURE__*/function () {
198
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
199
+ var _yield$import, createRequire, _yield$import2, readFile, file;
200
+ return _regenerator().w(function (_context) {
201
+ while (1) switch (_context.n) {
202
+ case 0:
203
+ _context.n = 1;
204
+ return function (specifier) {
205
+ return new Promise(function (r) {
206
+ return r("".concat(specifier));
207
+ }).then(function (s) {
208
+ return _interopRequireWildcard(require(s));
209
+ });
210
+ }(/* webpackIgnore: true */ /* @vite-ignore */['node', 'module'].join(':'));
211
+ case 1:
212
+ _yield$import = _context.v;
213
+ createRequire = _yield$import.createRequire;
214
+ _context.n = 2;
215
+ return function (specifier) {
216
+ return new Promise(function (r) {
217
+ return r("".concat(specifier));
218
+ }).then(function (s) {
219
+ return _interopRequireWildcard(require(s));
220
+ });
221
+ }(/* webpackIgnore: true */ /* @vite-ignore */['node', 'fs/promises'].join(':'));
222
+ case 2:
223
+ _yield$import2 = _context.v;
224
+ readFile = _yield$import2.readFile;
225
+ file = createRequire("".concat(process.cwd(), "/")).resolve('agglomerative-clustering/clustering.wasm');
226
+ return _context.a(2, readFile(file));
227
+ }
228
+ }, _callee);
229
+ }));
230
+ return function packagedWasm() {
231
+ return _ref2.apply(this, arguments);
232
+ };
233
+ }();
234
+
120
235
  // The module loads once: concurrent calls share one load, and a load that fails is forgotten, so it can be retried.
121
236
  var loading = null;
122
237
 
123
238
  // Calls get the instance they run on from `acquire` and keep using it, so replacing the current instance (see `discard`)
124
239
  // never changes the module under a call already in flight.
125
240
  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) {
241
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(options) {
242
+ var _i, _Object$keys, key, wasm;
243
+ return _regenerator().w(function (_context3) {
244
+ while (1) switch (_context3.n) {
130
245
  case 0:
131
246
  if (options === undefined) {
132
247
  options = {};
133
248
  }
134
249
  if ((0, _shared.isOptionsObject)(options)) {
135
- _context2.n = 1;
250
+ _context3.n = 1;
136
251
  break;
137
252
  }
138
253
  throw (0, _shared.fail)('invalid-argument', "Invalid options: expected an object.", 'options');
@@ -141,59 +256,90 @@ var acquire = /*#__PURE__*/function () {
141
256
  _i = 0, _Object$keys = Object.keys(options);
142
257
  case 2:
143
258
  if (!(_i < _Object$keys.length)) {
144
- _context2.n = 4;
259
+ _context3.n = 4;
145
260
  break;
146
261
  }
147
262
  key = _Object$keys[_i];
148
263
  if (!(key !== 'wasm')) {
149
- _context2.n = 3;
264
+ _context3.n = 3;
150
265
  break;
151
266
  }
152
267
  throw (0, _shared.fail)('invalid-argument', "Invalid option '".concat(key, "': expected wasm."), "options.".concat(key));
153
268
  case 3:
154
269
  _i++;
155
- _context2.n = 2;
270
+ _context3.n = 2;
156
271
  break;
157
272
  case 4:
158
- failed = new Promise(function (_, r) {
159
- reject = r;
160
- });
161
- failed["catch"](function () {});
162
- settings = wasmSettings(options.wasm, reject);
273
+ // Checked on every call, so a bad value is reported even when the module is already loaded.
274
+ wasmSettings(options.wasm, function () {});
163
275
  if (!Module) {
164
- _context2.n = 5;
276
+ _context3.n = 5;
165
277
  break;
166
278
  }
167
- return _context2.a(2, Module);
279
+ return _context3.a(2, Module);
168
280
  case 5:
169
281
  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) {
282
+ wasm = options.wasm;
283
+ loading = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
284
+ var instance, fallback, _t, _t2;
285
+ return _regenerator().w(function (_context2) {
286
+ while (1) switch (_context2.n) {
174
287
  case 0:
175
- _context.n = 1;
176
- return Promise.race([(0, _clustering["default"])(settings), failed])["catch"](function (error) {
177
- throw wasmLoadFailed(error);
178
- });
288
+ _context2.p = 0;
289
+ _context2.n = 1;
290
+ return instantiate(wasm);
179
291
  case 1:
180
- instance = _context.v;
292
+ instance = _context2.v;
293
+ _context2.n = 8;
294
+ break;
295
+ case 2:
296
+ _context2.p = 2;
297
+ _t = _context2.v;
298
+ if (!(wasm === undefined && isNodeLike)) {
299
+ _context2.n = 4;
300
+ break;
301
+ }
302
+ _context2.n = 3;
303
+ return packagedWasm()["catch"](function () {
304
+ return null;
305
+ });
306
+ case 3:
307
+ _t2 = _context2.v;
308
+ _context2.n = 5;
309
+ break;
310
+ case 4:
311
+ _t2 = null;
312
+ case 5:
313
+ fallback = _t2;
314
+ if (fallback) {
315
+ _context2.n = 6;
316
+ break;
317
+ }
318
+ throw wasmLoadFailed(_t, wasm !== undefined);
319
+ case 6:
320
+ _context2.n = 7;
321
+ return instantiate(fallback)["catch"](function () {
322
+ throw wasmLoadFailed(_t, false);
323
+ });
324
+ case 7:
325
+ instance = _context2.v;
326
+ case 8:
181
327
  checkWasmVersion(instance);
182
328
  Module = instance;
183
- return _context.a(2, instance);
329
+ return _context2.a(2, instance);
184
330
  }
185
- }, _callee);
331
+ }, _callee2, null, [[0, 2]]);
186
332
  }))()["catch"](function (error) {
187
333
  loading = null;
188
334
  throw error;
189
335
  });
190
336
  }
191
- return _context2.a(2, loading);
337
+ return _context3.a(2, loading);
192
338
  }
193
- }, _callee2);
339
+ }, _callee3);
194
340
  }));
195
341
  return function acquire(_x) {
196
- return _ref2.apply(this, arguments);
342
+ return _ref3.apply(this, arguments);
197
343
  };
198
344
  }();
199
345
 
@@ -201,19 +347,19 @@ var acquire = /*#__PURE__*/function () {
201
347
  // say where the .wasm file is (`wasm`), which must come before anything else. The first load that succeeds wins:
202
348
  // later calls, with or without options, get the same module.
203
349
  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) {
350
+ var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(options) {
351
+ return _regenerator().w(function (_context4) {
352
+ while (1) switch (_context4.n) {
207
353
  case 0:
208
- _context3.n = 1;
354
+ _context4.n = 1;
209
355
  return acquire(options);
210
356
  case 1:
211
- return _context3.a(2);
357
+ return _context4.a(2);
212
358
  }
213
- }, _callee3);
359
+ }, _callee4);
214
360
  }));
215
361
  return function init(_x2) {
216
- return _ref4.apply(this, arguments);
362
+ return _ref5.apply(this, arguments);
217
363
  };
218
364
  }();
219
365
 
@@ -344,7 +490,50 @@ var imageHint = function imageHint(image) {
344
490
  }
345
491
  return '';
346
492
  };
493
+
494
+ // A typed array whose buffer was transferred away (to a worker, say) is empty and can't be read or viewed.
495
+ var isDetached = function isDetached(view) {
496
+ var buffer = view.buffer;
497
+ if (typeof buffer.detached === 'boolean') {
498
+ return buffer.detached;
499
+ }
500
+ try {
501
+ new Uint8Array(buffer, 0, 0);
502
+ return false;
503
+ } catch (error) {
504
+ return true;
505
+ }
506
+ };
507
+
508
+ // The bytes a call works on are read when it is made, as with any function that takes an array: the caller may reuse
509
+ // or change the array as soon as the call returns. Once the module is loaded the bytes go into its memory before the
510
+ // call returns; until then (the first call) a copy is kept for when it has loaded.
511
+ var held = function held(data) {
512
+ return Module ? data : data.slice();
513
+ };
514
+
515
+ // width and height are copied to results and used for dithering, so each must make sense on its own: a whole number of
516
+ // pixels that divides the pixel count (and, when both are given, account for exactly the pixels in the data).
517
+ var checkSize = function checkSize(pixels, size, name, argument) {
518
+ if (size === undefined) {
519
+ return;
520
+ }
521
+ if (!Number.isInteger(size) || size < 0) {
522
+ throw (0, _shared.fail)('invalid-argument', "Invalid ".concat(name, ": must be a whole number of pixels."), argument);
523
+ }
524
+ if (pixels > 0 && (size === 0 || pixels % size !== 0)) {
525
+ throw (0, _shared.fail)('invalid-argument', "Invalid ".concat(name, ": ").concat(size, " does not divide the image's ").concat(pixels, " pixels into whole rows."), argument);
526
+ }
527
+ };
347
528
  var load = function load(image) {
529
+ if ((0, _shared.isClustering)(image) || (0, _shared.isPalette)(image)) {
530
+ var what = (0, _shared.isClustering)(image) ? 'a clustering (from getClustering)' : 'a palette';
531
+ var fix = (0, _shared.isClustering)(image) ? 'To cut a palette from it, use getPaletteFromClustering (or quantizeFromClustering to map an image with it).' : 'To map an image to it, use quantizeWithPalette(image, palette).';
532
+ throw (0, _shared.fail)('invalid-argument', "Invalid image: this is ".concat(what, ", not pixels. ").concat(fix), 'image');
533
+ }
534
+ if (ArrayBuffer.isView(image) && isDetached(image)) {
535
+ throw (0, _shared.fail)('invalid-argument', "Invalid image: its buffer was transferred away (detached), for example to a worker created with transfer: true, so it has no pixels left. Pass a copy if you need to keep using it.", 'image');
536
+ }
348
537
  if ((0, _shared.isUint8Array)(image)) {
349
538
  var encoded = sniffImageFormat(image);
350
539
  if (encoded) {
@@ -354,13 +543,16 @@ var load = function load(image) {
354
543
  throw (0, _shared.fail)('invalid-argument', "Invalid image data: length must be a multiple of 4 for 'rgba'.", 'image');
355
544
  }
356
545
  return {
357
- data: image,
546
+ data: held(image),
358
547
  format: 'rgba'
359
548
  };
360
549
  }
361
- if (typeof ImageData !== 'undefined' && image instanceof ImageData) {
550
+ if (isImageData(image)) {
551
+ if (isDetached(image.data)) {
552
+ throw (0, _shared.fail)('invalid-argument', "Invalid image: the ImageData's pixels were transferred away (detached), for example to a worker created with transfer: true.", 'image');
553
+ }
362
554
  return {
363
- data: bytes(image.data),
555
+ data: held(bytes(image.data)),
364
556
  format: 'rgba',
365
557
  width: image.width
366
558
  };
@@ -374,6 +566,9 @@ var load = function load(image) {
374
566
  if (!(0, _shared.isUint8Array)(image.data) && !(0, _shared.isUint8ClampedArray)(image.data)) {
375
567
  throw (0, _shared.fail)('invalid-argument', "Invalid image data: must be Uint8Array or Uint8ClampedArray.", 'image');
376
568
  }
569
+ if (isDetached(image.data)) {
570
+ throw (0, _shared.fail)('invalid-argument', "Invalid image data: its buffer was transferred away (detached), for example to a worker created with transfer: true, so it has no pixels left. Pass a copy if you need to keep using it.", 'image');
571
+ }
377
572
  if (!Object.keys(codes).includes(image.format)) {
378
573
  throw (0, _shared.fail)('invalid-argument', "Invalid format: must be 'rgb' or 'rgba'.", 'image');
379
574
  }
@@ -381,27 +576,20 @@ var load = function load(image) {
381
576
  if (image.data.length % channels !== 0) {
382
577
  throw (0, _shared.fail)('invalid-argument', "Invalid image data: length must be a multiple of ".concat(channels, " for '").concat(image.format, "'."), 'image');
383
578
  }
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');
579
+ var pixels = image.data.length / channels;
580
+ checkSize(pixels, image.width, 'image width', 'image');
581
+ checkSize(pixels, image.height, 'image height', 'image');
582
+ if (image.width !== undefined && image.height !== undefined && image.width * image.height !== pixels) {
583
+ throw (0, _shared.fail)('invalid-argument', "Invalid image: width x height (".concat(image.width, " x ").concat(image.height, ") does not match its ").concat(pixels, " pixels."), 'image');
396
584
  }
397
585
  return {
398
- data: bytes(image.data),
586
+ data: held(bytes(image.data)),
399
587
  format: image.format,
400
588
  width: image.width
401
589
  };
402
590
  };
403
591
  var getWrapper = function getWrapper(image) {
404
- if (typeof ImageData !== 'undefined' && image instanceof ImageData) {
592
+ if (isImageData(image)) {
405
593
  // The pixels stay in the colour space they came in ('display-p3' canvases), so the result must say so.
406
594
  var width = image.width,
407
595
  height = image.height,
@@ -497,8 +685,8 @@ var parseOptions = function parseOptions(options, allowed, image) {
497
685
  throw (0, _shared.fail)('invalid-argument', "Invalid options: expected an object.", 'options');
498
686
  }
499
687
  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];
688
+ for (var _i2 = 0, _Object$keys2 = Object.keys(options); _i2 < _Object$keys2.length; _i2++) {
689
+ var key = _Object$keys2[_i2];
502
690
  if (!allowed.includes(key)) {
503
691
  throw (0, _shared.fail)('invalid-argument', "Invalid option '".concat(key, "': expected one of ").concat(allowed.join(', '), "."), "options.".concat(key));
504
692
  }
@@ -525,6 +713,9 @@ var parseOptions = function parseOptions(options, allowed, image) {
525
713
  if (options.width !== undefined && image && image.width !== undefined && options.width !== image.width) {
526
714
  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');
527
715
  }
716
+ if (options.width !== undefined && image) {
717
+ checkSize(image.data.length / (image.format === 'rgb' ? 3 : 4), options.width, 'width', 'options.width');
718
+ }
528
719
  var width = options.width !== undefined ? options.width : image ? image.width : undefined;
529
720
  if (dither > 0) {
530
721
  var channels = image.format === 'rgb' ? 3 : 4;
@@ -577,6 +768,23 @@ var mapOptions = ['colorSpace', 'dither', 'width', 'signal'];
577
768
  var quantizeIndexOptions = quantizeOptions;
578
769
  var mapIndexOptions = [].concat(mapOptions, ['transparent']);
579
770
 
771
+ // An empty palette is what getPalette gives for an image with no visible pixels, and mapping such an image with it is
772
+ // fine. Mapping visible pixels to it has no answer (they would all come out black), so that is an error. `cutoff` is the
773
+ // alpha from which an RGBA pixel needs a colour.
774
+ var checkNotEmpty = function checkNotEmpty(palette, image, cutoff) {
775
+ if (palette.length > 0) {
776
+ return;
777
+ }
778
+ var data = image.data;
779
+ var visible = image.format === 'rgb' ? data.length > 0 : false;
780
+ for (var i = 3; !visible && i < data.length; i += 4) {
781
+ visible = data[i] >= cutoff;
782
+ }
783
+ if (visible) {
784
+ throw (0, _shared.fail)('invalid-argument', "Invalid palette: it is empty, but the image has visible pixels to map to it.", 'palette');
785
+ }
786
+ };
787
+
580
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.
581
789
  var matchingSpace = function matchingSpace(o, palette) {
582
790
  return o.colorSpaceGiven ? o.colorSpace : _shared.colorSpaces[(0, _shared.isPalette)(palette) ? palette.colorSpace : 'rgb'];
@@ -607,6 +815,11 @@ var call = function call(wasm, buffers, fn) {
607
815
  _iterator.f();
608
816
  }
609
817
  outputPointer = fn.apply(void 0, pointers);
818
+ // Only the functions that read a clustering return nothing: when its records don't fit together (see
819
+ // _get_palette_from_clustering), which a clustering whose checksum matches never does.
820
+ if (!outputPointer) {
821
+ throw (0, _shared.fail)('invalid-clustering', "Invalid clustering: its records don't fit together, so it is damaged or isn't a clustering. Cluster the image again.", 'clustering');
822
+ }
610
823
  return unpack(wasm, outputPointer);
611
824
  } catch (error) {
612
825
  if (!isAbort(error)) {
@@ -694,178 +907,205 @@ var isControlled = function isControlled(o) {
694
907
 
695
908
  // Clusters in slices and returns the clustering. `share` is the part of the whole call's progress this takes.
696
909
  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) {
910
+ var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(wasm, image, o, report, share) {
911
+ var imagePointer, job, aborted, progress, pointer, _t3;
912
+ return _regenerator().w(function (_context5) {
913
+ while (1) switch (_context5.n) {
701
914
  case 0:
702
915
  checkAborted(o.signal);
703
916
  imagePointer = wasm._malloc(image.data.length);
704
917
  if (imagePointer) {
705
- _context4.n = 1;
918
+ _context5.n = 1;
706
919
  break;
707
920
  }
708
921
  throw (0, _shared.fail)('out-of-memory', "Failed to allocate WASM memory.");
709
922
  case 1:
710
923
  job = 0;
711
924
  aborted = false;
712
- _context4.p = 2;
925
+ _context5.p = 2;
713
926
  wasm.HEAPU8.set(image.data, imagePointer);
714
927
  job = wasm._clustering_job_start(imagePointer, image.data.length, codes[image.format], o.colorSpace, o.precision, o.alpha, o.transparent);
715
928
  report(0);
716
929
  case 3:
717
930
  if (!true) {
718
- _context4.n = 6;
931
+ _context5.n = 6;
719
932
  break;
720
933
  }
721
934
  progress = wasm._clustering_job_step(job, sliceMs);
722
935
  if (!(progress >= 1000)) {
723
- _context4.n = 4;
936
+ _context5.n = 4;
724
937
  break;
725
938
  }
726
- return _context4.a(3, 6);
939
+ return _context5.a(3, 6);
727
940
  case 4:
728
941
  report(share * progress / 1000);
729
- _context4.n = 5;
942
+ _context5.n = 5;
730
943
  return yieldNow();
731
944
  case 5:
732
945
  checkAborted(o.signal);
733
- _context4.n = 3;
946
+ _context5.n = 3;
734
947
  break;
735
948
  case 6:
736
949
  pointer = wasm._clustering_job_result(job);
737
- _context4.p = 7;
738
- return _context4.a(2, unpack(wasm, pointer));
950
+ _context5.p = 7;
951
+ return _context5.a(2, unpack(wasm, pointer));
739
952
  case 8:
740
- _context4.p = 8;
953
+ _context5.p = 8;
741
954
  wasm._free(pointer);
742
- return _context4.f(8);
955
+ return _context5.f(8);
743
956
  case 9:
744
- _context4.n = 12;
957
+ _context5.n = 12;
745
958
  break;
746
959
  case 10:
747
- _context4.p = 10;
748
- _t = _context4.v;
749
- if (isAbort(_t)) {
750
- _context4.n = 11;
960
+ _context5.p = 10;
961
+ _t3 = _context5.v;
962
+ if (isAbort(_t3)) {
963
+ _context5.n = 11;
751
964
  break;
752
965
  }
753
- throw _t;
966
+ throw _t3;
754
967
  case 11:
755
968
  aborted = true;
756
969
  discard(wasm);
757
970
  throw outOfMemory();
758
971
  case 12:
759
- _context4.p = 12;
972
+ _context5.p = 12;
760
973
  if (!aborted) {
761
974
  if (job) {
762
975
  wasm._clustering_job_free(job);
763
976
  }
764
977
  wasm._free(imagePointer);
765
978
  }
766
- return _context4.f(12);
979
+ return _context5.f(12);
767
980
  case 13:
768
- return _context4.a(2);
981
+ return _context5.a(2);
769
982
  }
770
- }, _callee4, null, [[7,, 8, 9], [2, 10, 12, 13]]);
983
+ }, _callee5, null, [[7,, 8, 9], [2, 10, 12, 13]]);
771
984
  }));
772
985
  return function clusterInSlices(_x3, _x4, _x5, _x6, _x7) {
773
- return _ref5.apply(this, arguments);
986
+ return _ref6.apply(this, arguments);
774
987
  };
775
988
  }();
989
+
990
+ // Every function checks its arguments and takes its bytes (see `held`) before it waits for anything: when the module is
991
+ // already loaded, `Module || await acquire()` doesn't wait, and the work up to the first slice of a long clustering
992
+ // happens before the call returns.
993
+
776
994
  var getClustering = exports.getClustering = /*#__PURE__*/function () {
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) {
995
+ var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(image, options) {
996
+ var o, wasm, report, clustering, _t4;
997
+ return _regenerator().w(function (_context6) {
998
+ while (1) switch (_context6.n) {
781
999
  case 0:
782
- _context5.n = 1;
783
- return acquire();
784
- case 1:
785
- wasm = _context5.v;
786
1000
  image = load(image);
787
1001
  o = parseOptions(options, clusterOptions, image);
1002
+ _t4 = Module;
1003
+ if (_t4) {
1004
+ _context6.n = 2;
1005
+ break;
1006
+ }
1007
+ _context6.n = 1;
1008
+ return acquire();
1009
+ case 1:
1010
+ _t4 = _context6.v;
1011
+ case 2:
1012
+ wasm = _t4;
788
1013
  if (!isControlled(o)) {
789
- _context5.n = 3;
1014
+ _context6.n = 4;
790
1015
  break;
791
1016
  }
792
1017
  report = reporter(o);
793
- _context5.n = 2;
1018
+ _context6.n = 3;
794
1019
  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
1020
  case 3:
800
- return _context5.a(2, (0, _shared.markClustering)(call(wasm, [image.data], function (imagePointer) {
1021
+ clustering = _context6.v;
1022
+ report(1);
1023
+ return _context6.a(2, (0, _shared.markClustering)(clustering));
1024
+ case 4:
1025
+ return _context6.a(2, (0, _shared.markClustering)(call(wasm, [image.data], function (imagePointer) {
801
1026
  return wasm._get_clustering(imagePointer, image.data.length, codes[image.format], o.colorSpace, o.precision, o.alpha, o.transparent);
802
1027
  })));
803
1028
  }
804
- }, _callee5);
1029
+ }, _callee6);
805
1030
  }));
806
1031
  return function getClustering(_x8, _x9) {
807
- return _ref6.apply(this, arguments);
1032
+ return _ref7.apply(this, arguments);
808
1033
  };
809
1034
  }();
810
1035
  var getPalette = exports.getPalette = /*#__PURE__*/function () {
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) {
1036
+ var _ref8 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(image, k, options) {
1037
+ var o, wasm, report, clustering, palette, _t5;
1038
+ return _regenerator().w(function (_context7) {
1039
+ while (1) switch (_context7.n) {
815
1040
  case 0:
816
- _context6.n = 1;
817
- return acquire();
818
- case 1:
819
- wasm = _context6.v;
820
1041
  image = load(image);
821
1042
  k = check(k);
822
1043
  o = parseOptions(options, clusterOptions, image);
1044
+ _t5 = Module;
1045
+ if (_t5) {
1046
+ _context7.n = 2;
1047
+ break;
1048
+ }
1049
+ _context7.n = 1;
1050
+ return acquire();
1051
+ case 1:
1052
+ _t5 = _context7.v;
1053
+ case 2:
1054
+ wasm = _t5;
823
1055
  if (!isControlled(o)) {
824
- _context6.n = 3;
1056
+ _context7.n = 4;
825
1057
  break;
826
1058
  }
827
1059
  report = reporter(o);
828
- _context6.n = 2;
1060
+ _context7.n = 3;
829
1061
  return clusterInSlices(wasm, image, o, report, 0.99);
830
- case 2:
831
- clustering = _context6.v;
1062
+ case 3:
1063
+ clustering = _context7.v;
832
1064
  checkAborted(o.signal);
833
1065
  palette = call(wasm, [clustering], function (clusteringPointer) {
834
1066
  return wasm._get_palette_from_clustering(clusteringPointer, clustering.length, k);
835
1067
  });
836
1068
  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) {
1069
+ return _context7.a(2, (0, _shared.markPalette)(palette, (0, _shared.colorSpaceName)(o.colorSpace)));
1070
+ case 4:
1071
+ return _context7.a(2, (0, _shared.markPalette)(call(wasm, [image.data], function (imagePointer) {
840
1072
  return wasm._get_palette(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.transparent);
841
1073
  }), (0, _shared.colorSpaceName)(o.colorSpace)));
842
1074
  }
843
- }, _callee6);
1075
+ }, _callee7);
844
1076
  }));
845
1077
  return function getPalette(_x0, _x1, _x10) {
846
- return _ref7.apply(this, arguments);
1078
+ return _ref8.apply(this, arguments);
847
1079
  };
848
1080
  }();
849
1081
  var getPaletteFromClustering = exports.getPaletteFromClustering = /*#__PURE__*/function () {
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) {
1082
+ var _ref9 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(clustering, k) {
1083
+ var header, wasm, _t6;
1084
+ return _regenerator().w(function (_context8) {
1085
+ while (1) switch (_context8.n) {
854
1086
  case 0:
855
- _context7.n = 1;
856
- return acquire();
857
- case 1:
858
- wasm = _context7.v;
859
1087
  header = (0, _shared.readHeader)(clustering);
860
1088
  k = check(k);
861
- return _context7.a(2, (0, _shared.markPalette)(call(wasm, [clustering], function (clusteringPointer) {
1089
+ clustering = held(clustering);
1090
+ _t6 = Module;
1091
+ if (_t6) {
1092
+ _context8.n = 2;
1093
+ break;
1094
+ }
1095
+ _context8.n = 1;
1096
+ return acquire();
1097
+ case 1:
1098
+ _t6 = _context8.v;
1099
+ case 2:
1100
+ wasm = _t6;
1101
+ return _context8.a(2, (0, _shared.markPalette)(call(wasm, [clustering], function (clusteringPointer) {
862
1102
  return wasm._get_palette_from_clustering(clusteringPointer, clustering.length, k);
863
1103
  }), (0, _shared.colorSpaceName)(header.colorSpace)));
864
1104
  }
865
- }, _callee7);
1105
+ }, _callee8);
866
1106
  }));
867
1107
  return function getPaletteFromClustering(_x11, _x12) {
868
- return _ref8.apply(this, arguments);
1108
+ return _ref9.apply(this, arguments);
869
1109
  };
870
1110
  }();
871
1111
 
@@ -884,23 +1124,31 @@ var weightsOf = function weightsOf(wasm, clustering, header, k) {
884
1124
  // arrays line up by construction. Weights are pixels: fully transparent pixels count 0 and partly transparent ones in
885
1125
  // proportion to their opacity, as in clustering. Shares are the weights divided by their total, so they sum to 1.
886
1126
  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) {
1127
+ var _ref0 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(clustering, k) {
1128
+ var header, wasm, palette, weights, total, _iterator3, _step3, weight, shares, i, _t7;
1129
+ return _regenerator().w(function (_context9) {
1130
+ while (1) switch (_context9.n) {
891
1131
  case 0:
892
- _context8.n = 1;
893
- return acquire();
894
- case 1:
895
- wasm = _context8.v;
896
1132
  header = (0, _shared.readHeader)(clustering);
897
1133
  k = check(k);
898
1134
  if (!(header.version < 3)) {
899
- _context8.n = 2;
1135
+ _context9.n = 1;
900
1136
  break;
901
1137
  }
902
1138
  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');
1139
+ case 1:
1140
+ clustering = held(clustering);
1141
+ _t7 = Module;
1142
+ if (_t7) {
1143
+ _context9.n = 3;
1144
+ break;
1145
+ }
1146
+ _context9.n = 2;
1147
+ return acquire();
903
1148
  case 2:
1149
+ _t7 = _context9.v;
1150
+ case 3:
1151
+ wasm = _t7;
904
1152
  palette = (0, _shared.markPalette)(call(wasm, [clustering], function (clusteringPointer) {
905
1153
  return wasm._get_palette_from_clustering(clusteringPointer, clustering.length, k);
906
1154
  }), (0, _shared.colorSpaceName)(header.colorSpace));
@@ -923,41 +1171,41 @@ var getSwatchesFromClustering = exports.getSwatchesFromClustering = /*#__PURE__*
923
1171
  shares[i] = weights[i] / total;
924
1172
  }
925
1173
  }
926
- return _context8.a(2, {
1174
+ return _context9.a(2, {
927
1175
  palette: palette,
928
1176
  weights: weights,
929
1177
  shares: shares
930
1178
  });
931
1179
  }
932
- }, _callee8);
1180
+ }, _callee9);
933
1181
  }));
934
1182
  return function getSwatchesFromClustering(_x13, _x14) {
935
- return _ref9.apply(this, arguments);
1183
+ return _ref0.apply(this, arguments);
936
1184
  };
937
1185
  }();
938
1186
 
939
1187
  // The same for an image: clusters it, then cuts, like getPalette.
940
1188
  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) {
1189
+ var _ref1 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(image, k, options) {
1190
+ var _t8, _t9, _t0;
1191
+ return _regenerator().w(function (_context0) {
1192
+ while (1) switch (_context0.n) {
945
1193
  case 0:
946
1194
  // Arguments are checked in the order they are given (image, then k, then options), as in every other function.
947
1195
  load(image);
948
1196
  k = check(k);
949
- _t2 = getSwatchesFromClustering;
950
- _context9.n = 1;
1197
+ _t8 = getSwatchesFromClustering;
1198
+ _context0.n = 1;
951
1199
  return getClustering(image, options);
952
1200
  case 1:
953
- _t3 = _context9.v;
954
- _t4 = k;
955
- return _context9.a(2, _t2(_t3, _t4));
1201
+ _t9 = _context0.v;
1202
+ _t0 = k;
1203
+ return _context0.a(2, _t8(_t9, _t0));
956
1204
  }
957
- }, _callee9);
1205
+ }, _callee0);
958
1206
  }));
959
1207
  return function getSwatches(_x15, _x16, _x17) {
960
- return _ref0.apply(this, arguments);
1208
+ return _ref1.apply(this, arguments);
961
1209
  };
962
1210
  }();
963
1211
 
@@ -965,10 +1213,10 @@ var getSwatches = exports.getSwatches = /*#__PURE__*/function () {
965
1213
  // squared error per channel of the k-colour clustering: each pixel's squared distance to its cluster's colour,
966
1214
  // weighted as in clustering, in 8-bit RGB values (RGB) or OKLab units (OKLab).
967
1215
  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) {
1216
+ var _ref10 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1(clustering) {
1217
+ var header, colors, info, view, pixels, total, remaining, i, _i3, _i4;
1218
+ return _regenerator().w(function (_context1) {
1219
+ while (1) switch (_context1.n) {
972
1220
  case 0:
973
1221
  header = (0, _shared.readHeader)(clustering);
974
1222
  colors = header.merges + (clustering.length >= header.headerLength + 3 ? 1 : 0);
@@ -983,10 +1231,10 @@ var getClusteringInfo = exports.getClusteringInfo = /*#__PURE__*/function () {
983
1231
  explained: null
984
1232
  };
985
1233
  if (!(header.version < 3)) {
986
- _context0.n = 1;
1234
+ _context1.n = 1;
987
1235
  break;
988
1236
  }
989
- return _context0.a(2, info);
1237
+ return _context1.a(2, info);
990
1238
  case 1:
991
1239
  view = new DataView(clustering.buffer, clustering.byteOffset, clustering.byteLength);
992
1240
  pixels = Math.pow(2, clustering[7]) / (clustering[6] === 1 ? 255 : 1);
@@ -1003,205 +1251,273 @@ var getClusteringInfo = exports.getClusteringInfo = /*#__PURE__*/function () {
1003
1251
  remaining += view.getFloat32(header.headerLength + 3 + i * header.recordLength + 17, true);
1004
1252
  info.mse[i] = remaining / total / 3;
1005
1253
  }
1254
+ // Errors are sums of squares, so a cost that is negative or not a number can only come from damaged bytes (a clustering
1255
+ // from 3.0.0-beta.1 has no checksum to catch them first).
1256
+ _i3 = 0;
1257
+ case 2:
1258
+ if (!(_i3 < colors)) {
1259
+ _context1.n = 4;
1260
+ break;
1261
+ }
1262
+ if (!(!(info.mse[_i3] >= 0) || !Number.isFinite(info.mse[_i3]))) {
1263
+ _context1.n = 3;
1264
+ break;
1265
+ }
1266
+ throw (0, _shared.fail)('invalid-clustering', "Invalid clustering: its recorded errors are not valid numbers, so it is damaged. Cluster the image again.", 'clustering');
1267
+ case 3:
1268
+ _i3++;
1269
+ _context1.n = 2;
1270
+ break;
1271
+ case 4:
1006
1272
  // The share of the image's variance each palette size keeps; 1 throughout for an image of one colour.
1007
1273
  for (_i4 = 0; _i4 < colors; _i4++) {
1008
1274
  info.explained[_i4] = info.mse[0] > 0 ? Math.max(0, Math.min(1, 1 - info.mse[_i4] / info.mse[0])) : 1;
1009
1275
  }
1010
- return _context0.a(2, info);
1276
+ return _context1.a(2, info);
1011
1277
  }
1012
- }, _callee0);
1278
+ }, _callee1);
1013
1279
  }));
1014
1280
  return function getClusteringInfo(_x18) {
1015
- return _ref1.apply(this, arguments);
1281
+ return _ref10.apply(this, arguments);
1016
1282
  };
1017
1283
  }();
1018
1284
  var quantize = exports.quantize = /*#__PURE__*/function () {
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) {
1285
+ var _ref11 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10(image, k, options) {
1286
+ var wrap, o, wasm, report, clustering, mapped, _t1;
1287
+ return _regenerator().w(function (_context10) {
1288
+ while (1) switch (_context10.n) {
1023
1289
  case 0:
1024
- _context1.n = 1;
1025
- return acquire();
1026
- case 1:
1027
- wasm = _context1.v;
1028
1290
  wrap = getWrapper(image);
1029
1291
  image = load(image);
1030
1292
  k = check(k);
1031
1293
  o = parseOptions(options, quantizeOptions, image);
1294
+ _t1 = Module;
1295
+ if (_t1) {
1296
+ _context10.n = 2;
1297
+ break;
1298
+ }
1299
+ _context10.n = 1;
1300
+ return acquire();
1301
+ case 1:
1302
+ _t1 = _context10.v;
1303
+ case 2:
1304
+ wasm = _t1;
1032
1305
  if (!isControlled(o)) {
1033
- _context1.n = 3;
1306
+ _context10.n = 4;
1034
1307
  break;
1035
1308
  }
1036
1309
  report = reporter(o);
1037
- _context1.n = 2;
1310
+ _context10.n = 3;
1038
1311
  return clusterInSlices(wasm, image, o, report, 0.9);
1039
- case 2:
1040
- clustering = _context1.v;
1312
+ case 3:
1313
+ clustering = _context10.v;
1041
1314
  checkAborted(o.signal);
1042
1315
  mapped = wrap(call(wasm, [image.data, clustering], function (imagePointer, clusteringPointer) {
1043
1316
  return wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, o.colorSpace, o.dither, o.width);
1044
1317
  }));
1045
1318
  report(1);
1046
- return _context1.a(2, mapped);
1047
- case 3:
1048
- return _context1.a(2, wrap(call(wasm, [image.data], function (imagePointer) {
1319
+ return _context10.a(2, mapped);
1320
+ case 4:
1321
+ return _context10.a(2, wrap(call(wasm, [image.data], function (imagePointer) {
1049
1322
  return wasm._quantize(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.dither, o.width, o.transparent);
1050
1323
  })));
1051
1324
  }
1052
- }, _callee1);
1325
+ }, _callee10);
1053
1326
  }));
1054
1327
  return function quantize(_x19, _x20, _x21) {
1055
- return _ref10.apply(this, arguments);
1328
+ return _ref11.apply(this, arguments);
1056
1329
  };
1057
1330
  }();
1058
1331
  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) {
1332
+ var _ref12 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11(image, clustering, k, options) {
1333
+ var wrap, o, wasm, colorSpace, result, _t10;
1334
+ return _regenerator().w(function (_context11) {
1335
+ while (1) switch (_context11.n) {
1063
1336
  case 0:
1064
- _context10.n = 1;
1065
- return acquire();
1066
- case 1:
1067
- wasm = _context10.v;
1068
1337
  wrap = getWrapper(image);
1069
1338
  image = load(image);
1070
1339
  (0, _shared.readHeader)(clustering);
1071
1340
  k = check(k);
1072
1341
  o = parseOptions(options, mapOptions, image);
1342
+ clustering = held(clustering);
1343
+ _t10 = Module;
1344
+ if (_t10) {
1345
+ _context11.n = 2;
1346
+ break;
1347
+ }
1348
+ _context11.n = 1;
1349
+ return acquire();
1350
+ case 1:
1351
+ _t10 = _context11.v;
1352
+ case 2:
1353
+ wasm = _t10;
1073
1354
  checkAborted(o.signal);
1074
1355
  colorSpace = o.colorSpaceGiven ? o.colorSpace : -1;
1075
1356
  result = wrap(call(wasm, [image.data, clustering], function (imagePointer, clusteringPointer) {
1076
1357
  return wasm._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.width);
1077
1358
  }));
1078
- return _context10.a(2, result);
1359
+ return _context11.a(2, result);
1079
1360
  }
1080
- }, _callee10);
1361
+ }, _callee11);
1081
1362
  }));
1082
1363
  return function quantizeFromClustering(_x22, _x23, _x24, _x25) {
1083
- return _ref11.apply(this, arguments);
1364
+ return _ref12.apply(this, arguments);
1084
1365
  };
1085
1366
  }();
1086
1367
  var quantizeWithPalette = exports.quantizeWithPalette = /*#__PURE__*/function () {
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) {
1368
+ var _ref13 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee12(image, palette, options) {
1369
+ var wrap, o, space, wasm, result, _t11;
1370
+ return _regenerator().w(function (_context12) {
1371
+ while (1) switch (_context12.n) {
1091
1372
  case 0:
1092
- _context11.n = 1;
1093
- return acquire();
1094
- case 1:
1095
- wasm = _context11.v;
1096
1373
  wrap = getWrapper(image);
1097
1374
  image = load(image);
1098
1375
  (0, _shared.checkPalette)(palette);
1099
1376
  o = parseOptions(options, mapOptions, image);
1377
+ checkNotEmpty(palette, image, 1);
1378
+ space = matchingSpace(o, palette);
1379
+ palette = held(palette);
1380
+ _t11 = Module;
1381
+ if (_t11) {
1382
+ _context12.n = 2;
1383
+ break;
1384
+ }
1385
+ _context12.n = 1;
1386
+ return acquire();
1387
+ case 1:
1388
+ _t11 = _context12.v;
1389
+ case 2:
1390
+ wasm = _t11;
1100
1391
  checkAborted(o.signal);
1101
1392
  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);
1393
+ return wasm._quantize_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, space, o.dither, o.width);
1103
1394
  }));
1104
- return _context11.a(2, result);
1395
+ return _context12.a(2, result);
1105
1396
  }
1106
- }, _callee11);
1397
+ }, _callee12);
1107
1398
  }));
1108
1399
  return function quantizeWithPalette(_x26, _x27, _x28) {
1109
- return _ref12.apply(this, arguments);
1400
+ return _ref13.apply(this, arguments);
1110
1401
  };
1111
1402
  }();
1112
1403
  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) {
1404
+ var _ref14 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee13(image, k, options) {
1405
+ var original, o, wasm, report, clustering, indexed, _t12;
1406
+ return _regenerator().w(function (_context13) {
1407
+ while (1) switch (_context13.n) {
1117
1408
  case 0:
1118
- _context12.n = 1;
1119
- return acquire();
1120
- case 1:
1121
- wasm = _context12.v;
1122
1409
  original = image;
1123
1410
  image = load(image);
1124
1411
  k = check(k);
1125
1412
  o = parseOptions(options, quantizeIndexOptions, image);
1413
+ _t12 = Module;
1414
+ if (_t12) {
1415
+ _context13.n = 2;
1416
+ break;
1417
+ }
1418
+ _context13.n = 1;
1419
+ return acquire();
1420
+ case 1:
1421
+ _t12 = _context13.v;
1422
+ case 2:
1423
+ wasm = _t12;
1126
1424
  if (!isControlled(o)) {
1127
- _context12.n = 3;
1425
+ _context13.n = 4;
1128
1426
  break;
1129
1427
  }
1130
1428
  report = reporter(o);
1131
- _context12.n = 2;
1429
+ _context13.n = 3;
1132
1430
  return clusterInSlices(wasm, image, o, report, 0.9);
1133
- case 2:
1134
- clustering = _context12.v;
1431
+ case 3:
1432
+ clustering = _context13.v;
1135
1433
  checkAborted(o.signal);
1136
1434
  indexed = unpackIndices(call(wasm, [image.data, clustering], function (imagePointer, clusteringPointer) {
1137
1435
  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
1436
  }), original, (0, _shared.colorSpaceName)(o.colorSpace));
1139
1437
  report(1);
1140
- return _context12.a(2, indexed);
1141
- case 3:
1142
- return _context12.a(2, unpackIndices(call(wasm, [image.data], function (imagePointer) {
1438
+ return _context13.a(2, indexed);
1439
+ case 4:
1440
+ return _context13.a(2, unpackIndices(call(wasm, [image.data], function (imagePointer) {
1143
1441
  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
1442
  }), original, (0, _shared.colorSpaceName)(o.colorSpace)));
1145
1443
  }
1146
- }, _callee12);
1444
+ }, _callee13);
1147
1445
  }));
1148
1446
  return function quantizeToIndices(_x29, _x30, _x31) {
1149
- return _ref13.apply(this, arguments);
1447
+ return _ref14.apply(this, arguments);
1150
1448
  };
1151
1449
  }();
1152
1450
  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) {
1451
+ var _ref15 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee14(image, clustering, k, options) {
1452
+ var original, header, o, wasm, colorSpace, cutoff, result, _t13;
1453
+ return _regenerator().w(function (_context14) {
1454
+ while (1) switch (_context14.n) {
1157
1455
  case 0:
1158
- _context13.n = 1;
1159
- return acquire();
1160
- case 1:
1161
- wasm = _context13.v;
1162
1456
  original = image;
1163
1457
  image = load(image);
1164
1458
  header = (0, _shared.readHeader)(clustering);
1165
1459
  k = check(k);
1166
1460
  o = parseOptions(options, mapIndexOptions, image);
1461
+ clustering = held(clustering);
1462
+ _t13 = Module;
1463
+ if (_t13) {
1464
+ _context14.n = 2;
1465
+ break;
1466
+ }
1467
+ _context14.n = 1;
1468
+ return acquire();
1469
+ case 1:
1470
+ _t13 = _context14.v;
1471
+ case 2:
1472
+ wasm = _t13;
1167
1473
  checkAborted(o.signal);
1168
1474
  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
1475
  cutoff = o.transparentGiven ? o.transparent : header.transparent;
1170
1476
  result = unpackIndices(call(wasm, [image.data, clustering], function (imagePointer, clusteringPointer) {
1171
1477
  return wasm._quantize_indices_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.width, cutoff);
1172
1478
  }), original, (0, _shared.colorSpaceName)(header.colorSpace));
1173
- return _context13.a(2, result);
1479
+ return _context14.a(2, result);
1174
1480
  }
1175
- }, _callee13);
1481
+ }, _callee14);
1176
1482
  }));
1177
1483
  return function quantizeToIndicesFromClustering(_x32, _x33, _x34, _x35) {
1178
- return _ref14.apply(this, arguments);
1484
+ return _ref15.apply(this, arguments);
1179
1485
  };
1180
1486
  }();
1181
1487
  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) {
1488
+ var _ref16 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee15(image, palette, options) {
1489
+ var original, o, space, paletteSpace, wasm, result, _t14;
1490
+ return _regenerator().w(function (_context15) {
1491
+ while (1) switch (_context15.n) {
1186
1492
  case 0:
1187
- _context14.n = 1;
1188
- return acquire();
1189
- case 1:
1190
- wasm = _context14.v;
1191
1493
  original = image;
1192
1494
  image = load(image);
1193
1495
  (0, _shared.checkPalette)(palette);
1194
1496
  o = parseOptions(options, mapIndexOptions, image);
1195
- checkAborted(o.signal);
1497
+ checkNotEmpty(palette, image, o.transparent > 0 ? o.transparent : 1);
1196
1498
  space = matchingSpace(o, palette);
1499
+ paletteSpace = (0, _shared.isPalette)(palette) ? palette.colorSpace : (0, _shared.colorSpaceName)(space);
1500
+ palette = held(palette);
1501
+ _t14 = Module;
1502
+ if (_t14) {
1503
+ _context15.n = 2;
1504
+ break;
1505
+ }
1506
+ _context15.n = 1;
1507
+ return acquire();
1508
+ case 1:
1509
+ _t14 = _context15.v;
1510
+ case 2:
1511
+ wasm = _t14;
1512
+ checkAborted(o.signal);
1197
1513
  result = unpackIndices(call(wasm, [image.data, palette], function (imagePointer, palettePointer) {
1198
1514
  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);
1515
+ }), original, paletteSpace);
1516
+ return _context15.a(2, result);
1201
1517
  }
1202
- }, _callee14);
1518
+ }, _callee15);
1203
1519
  }));
1204
1520
  return function quantizeToIndicesWithPalette(_x36, _x37, _x38) {
1205
- return _ref15.apply(this, arguments);
1521
+ return _ref16.apply(this, arguments);
1206
1522
  };
1207
1523
  }();