agglomerative-clustering 2.0.0-beta.1 → 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
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
2
|
+
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."); }
|
|
3
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
4
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
5
|
+
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); }
|
|
6
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
7
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
8
|
+
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; } }
|
|
9
|
+
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; }
|
|
10
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
11
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
12
|
+
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 }; })(); }
|
|
13
|
+
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); }
|
|
14
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
15
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
16
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
17
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
18
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
19
|
+
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); }
|
|
20
|
+
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); }); }; }
|
|
21
|
+
// Runs inside the worker started by worker.js: receives calls, runs them with the main entry, and posts results
|
|
22
|
+
// back, transferring every result buffer. Imports `./index.mjs`, which exists only in dist/ (like clustering.js).
|
|
23
|
+
import * as ac from './index.mjs';
|
|
24
|
+
import { MARK, mark, revive, isPalette } from "./shared.mjs";
|
|
25
|
+
|
|
26
|
+
// Images kept in the worker by `cluster`, with their clusterings, by handle id.
|
|
27
|
+
var handles = new Map();
|
|
28
|
+
var handle = function handle(id) {
|
|
29
|
+
var entry = handles.get(id);
|
|
30
|
+
if (!entry) {
|
|
31
|
+
throw Object.assign(new Error("Invalid handle: it was released, or belongs to another worker."), {
|
|
32
|
+
code: 'invalid-handle'
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return entry;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// A palette doesn't say which colour space it was made in. A handle's palette almost always came from the handle itself,
|
|
39
|
+
// so mapping with it should match in the space the handle was clustered in, as handle.quantize does, unless the caller
|
|
40
|
+
// names one. Options that aren't an ordinary object are passed on as they are for the main entry to reject.
|
|
41
|
+
var inClusteringSpace = /*#__PURE__*/function () {
|
|
42
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(id, options, palette) {
|
|
43
|
+
var _yield$ac$getClusteri, colorSpace;
|
|
44
|
+
return _regenerator().w(function (_context) {
|
|
45
|
+
while (1) switch (_context.n) {
|
|
46
|
+
case 0:
|
|
47
|
+
if (!(options !== undefined && Object.prototype.toString.call(options) !== '[object Object]')) {
|
|
48
|
+
_context.n = 1;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
return _context.a(2, options);
|
|
52
|
+
case 1:
|
|
53
|
+
if (!(isPalette(palette) && !(options && options.colorSpace !== undefined))) {
|
|
54
|
+
_context.n = 2;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
return _context.a(2, options);
|
|
58
|
+
case 2:
|
|
59
|
+
_context.n = 3;
|
|
60
|
+
return ac.getClusteringInfo(handle(id).clustering);
|
|
61
|
+
case 3:
|
|
62
|
+
_yield$ac$getClusteri = _context.v;
|
|
63
|
+
colorSpace = _yield$ac$getClusteri.colorSpace;
|
|
64
|
+
return _context.a(2, _objectSpread(_objectSpread({}, options), {}, {
|
|
65
|
+
colorSpace: options && options.colorSpace !== undefined ? options.colorSpace : colorSpace
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
}, _callee);
|
|
69
|
+
}));
|
|
70
|
+
return function inClusteringSpace(_x, _x2, _x3) {
|
|
71
|
+
return _ref.apply(this, arguments);
|
|
72
|
+
};
|
|
73
|
+
}();
|
|
74
|
+
|
|
75
|
+
// Palettes and clusterings cross the boundary as markers (shared.js): arguments are revived here, and results are marked
|
|
76
|
+
// before they are posted.
|
|
77
|
+
// The functions of the main entry, by name (not its classes and constants).
|
|
78
|
+
var functions = Object.fromEntries(Object.entries(ac).filter(function (_ref2) {
|
|
79
|
+
var _ref3 = _slicedToArray(_ref2, 2),
|
|
80
|
+
name = _ref3[0],
|
|
81
|
+
value = _ref3[1];
|
|
82
|
+
return typeof value === 'function' && !/^[A-Z]/.test(name);
|
|
83
|
+
}));
|
|
84
|
+
var methods = _objectSpread(_objectSpread({}, functions), {}, {
|
|
85
|
+
cluster: function () {
|
|
86
|
+
var _cluster = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(id, image, options) {
|
|
87
|
+
var _t, _t2, _t3, _t4, _t5;
|
|
88
|
+
return _regenerator().w(function (_context2) {
|
|
89
|
+
while (1) switch (_context2.n) {
|
|
90
|
+
case 0:
|
|
91
|
+
_t = handles;
|
|
92
|
+
_t2 = id;
|
|
93
|
+
_t3 = image;
|
|
94
|
+
_context2.n = 1;
|
|
95
|
+
return ac.getClustering(image, options);
|
|
96
|
+
case 1:
|
|
97
|
+
_t4 = _context2.v;
|
|
98
|
+
_t5 = {
|
|
99
|
+
image: _t3,
|
|
100
|
+
clustering: _t4
|
|
101
|
+
};
|
|
102
|
+
_t.set.call(_t, _t2, _t5);
|
|
103
|
+
case 2:
|
|
104
|
+
return _context2.a(2);
|
|
105
|
+
}
|
|
106
|
+
}, _callee2);
|
|
107
|
+
}));
|
|
108
|
+
function cluster(_x4, _x5, _x6) {
|
|
109
|
+
return _cluster.apply(this, arguments);
|
|
110
|
+
}
|
|
111
|
+
return cluster;
|
|
112
|
+
}(),
|
|
113
|
+
release: function () {
|
|
114
|
+
var _release = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(id) {
|
|
115
|
+
return _regenerator().w(function (_context3) {
|
|
116
|
+
while (1) switch (_context3.n) {
|
|
117
|
+
case 0:
|
|
118
|
+
handles["delete"](id);
|
|
119
|
+
case 1:
|
|
120
|
+
return _context3.a(2);
|
|
121
|
+
}
|
|
122
|
+
}, _callee3);
|
|
123
|
+
}));
|
|
124
|
+
function release(_x7) {
|
|
125
|
+
return _release.apply(this, arguments);
|
|
126
|
+
}
|
|
127
|
+
return release;
|
|
128
|
+
}(),
|
|
129
|
+
handleGetClustering: function () {
|
|
130
|
+
var _handleGetClustering = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(id) {
|
|
131
|
+
return _regenerator().w(function (_context4) {
|
|
132
|
+
while (1) switch (_context4.n) {
|
|
133
|
+
case 0:
|
|
134
|
+
return _context4.a(2, new ac.Clustering(handle(id).clustering));
|
|
135
|
+
}
|
|
136
|
+
}, _callee4);
|
|
137
|
+
}));
|
|
138
|
+
function handleGetClustering(_x8) {
|
|
139
|
+
return _handleGetClustering.apply(this, arguments);
|
|
140
|
+
}
|
|
141
|
+
return handleGetClustering;
|
|
142
|
+
}(),
|
|
143
|
+
handleGetPalette: function () {
|
|
144
|
+
var _handleGetPalette = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(id, k) {
|
|
145
|
+
return _regenerator().w(function (_context5) {
|
|
146
|
+
while (1) switch (_context5.n) {
|
|
147
|
+
case 0:
|
|
148
|
+
return _context5.a(2, ac.getPaletteFromClustering(handle(id).clustering, k));
|
|
149
|
+
}
|
|
150
|
+
}, _callee5);
|
|
151
|
+
}));
|
|
152
|
+
function handleGetPalette(_x9, _x0) {
|
|
153
|
+
return _handleGetPalette.apply(this, arguments);
|
|
154
|
+
}
|
|
155
|
+
return handleGetPalette;
|
|
156
|
+
}(),
|
|
157
|
+
handleGetSwatches: function () {
|
|
158
|
+
var _handleGetSwatches = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(id, k) {
|
|
159
|
+
return _regenerator().w(function (_context6) {
|
|
160
|
+
while (1) switch (_context6.n) {
|
|
161
|
+
case 0:
|
|
162
|
+
return _context6.a(2, ac.getSwatchesFromClustering(handle(id).clustering, k));
|
|
163
|
+
}
|
|
164
|
+
}, _callee6);
|
|
165
|
+
}));
|
|
166
|
+
function handleGetSwatches(_x1, _x10) {
|
|
167
|
+
return _handleGetSwatches.apply(this, arguments);
|
|
168
|
+
}
|
|
169
|
+
return handleGetSwatches;
|
|
170
|
+
}(),
|
|
171
|
+
handleGetClusteringInfo: function () {
|
|
172
|
+
var _handleGetClusteringInfo = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(id) {
|
|
173
|
+
return _regenerator().w(function (_context7) {
|
|
174
|
+
while (1) switch (_context7.n) {
|
|
175
|
+
case 0:
|
|
176
|
+
return _context7.a(2, ac.getClusteringInfo(handle(id).clustering));
|
|
177
|
+
}
|
|
178
|
+
}, _callee7);
|
|
179
|
+
}));
|
|
180
|
+
function handleGetClusteringInfo(_x11) {
|
|
181
|
+
return _handleGetClusteringInfo.apply(this, arguments);
|
|
182
|
+
}
|
|
183
|
+
return handleGetClusteringInfo;
|
|
184
|
+
}(),
|
|
185
|
+
handleQuantize: function () {
|
|
186
|
+
var _handleQuantize = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(id, k, options) {
|
|
187
|
+
return _regenerator().w(function (_context8) {
|
|
188
|
+
while (1) switch (_context8.n) {
|
|
189
|
+
case 0:
|
|
190
|
+
return _context8.a(2, ac.quantizeFromClustering(handle(id).image, handle(id).clustering, k, options));
|
|
191
|
+
}
|
|
192
|
+
}, _callee8);
|
|
193
|
+
}));
|
|
194
|
+
function handleQuantize(_x12, _x13, _x14) {
|
|
195
|
+
return _handleQuantize.apply(this, arguments);
|
|
196
|
+
}
|
|
197
|
+
return handleQuantize;
|
|
198
|
+
}(),
|
|
199
|
+
handleQuantizeToIndices: function () {
|
|
200
|
+
var _handleQuantizeToIndices = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(id, k, options) {
|
|
201
|
+
return _regenerator().w(function (_context9) {
|
|
202
|
+
while (1) switch (_context9.n) {
|
|
203
|
+
case 0:
|
|
204
|
+
return _context9.a(2, ac.quantizeToIndicesFromClustering(handle(id).image, handle(id).clustering, k, options));
|
|
205
|
+
}
|
|
206
|
+
}, _callee9);
|
|
207
|
+
}));
|
|
208
|
+
function handleQuantizeToIndices(_x15, _x16, _x17) {
|
|
209
|
+
return _handleQuantizeToIndices.apply(this, arguments);
|
|
210
|
+
}
|
|
211
|
+
return handleQuantizeToIndices;
|
|
212
|
+
}(),
|
|
213
|
+
handleQuantizeWithPalette: function () {
|
|
214
|
+
var _handleQuantizeWithPalette = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(id, palette, options) {
|
|
215
|
+
var _t6, _t7, _t8, _t9;
|
|
216
|
+
return _regenerator().w(function (_context0) {
|
|
217
|
+
while (1) switch (_context0.n) {
|
|
218
|
+
case 0:
|
|
219
|
+
_t6 = ac;
|
|
220
|
+
_t7 = handle(id).image;
|
|
221
|
+
_t8 = palette;
|
|
222
|
+
_context0.n = 1;
|
|
223
|
+
return inClusteringSpace(id, options, palette);
|
|
224
|
+
case 1:
|
|
225
|
+
_t9 = _context0.v;
|
|
226
|
+
return _context0.a(2, _t6.quantizeWithPalette.call(_t6, _t7, _t8, _t9));
|
|
227
|
+
}
|
|
228
|
+
}, _callee0);
|
|
229
|
+
}));
|
|
230
|
+
function handleQuantizeWithPalette(_x18, _x19, _x20) {
|
|
231
|
+
return _handleQuantizeWithPalette.apply(this, arguments);
|
|
232
|
+
}
|
|
233
|
+
return handleQuantizeWithPalette;
|
|
234
|
+
}(),
|
|
235
|
+
handleQuantizeToIndicesWithPalette: function () {
|
|
236
|
+
var _handleQuantizeToIndicesWithPalette = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1(id, palette, options) {
|
|
237
|
+
var _t0, _t1, _t10, _t11;
|
|
238
|
+
return _regenerator().w(function (_context1) {
|
|
239
|
+
while (1) switch (_context1.n) {
|
|
240
|
+
case 0:
|
|
241
|
+
_t0 = ac;
|
|
242
|
+
_t1 = handle(id).image;
|
|
243
|
+
_t10 = palette;
|
|
244
|
+
_context1.n = 1;
|
|
245
|
+
return inClusteringSpace(id, options, palette);
|
|
246
|
+
case 1:
|
|
247
|
+
_t11 = _context1.v;
|
|
248
|
+
return _context1.a(2, _t0.quantizeToIndicesWithPalette.call(_t0, _t1, _t10, _t11));
|
|
249
|
+
}
|
|
250
|
+
}, _callee1);
|
|
251
|
+
}));
|
|
252
|
+
function handleQuantizeToIndicesWithPalette(_x21, _x22, _x23) {
|
|
253
|
+
return _handleQuantizeToIndicesWithPalette.apply(this, arguments);
|
|
254
|
+
}
|
|
255
|
+
return handleQuantizeToIndicesWithPalette;
|
|
256
|
+
}()
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// The buffers in a result, to transfer rather than copy. Results are freshly allocated, so nothing here uses them.
|
|
260
|
+
var buffers = function buffers(value) {
|
|
261
|
+
if (ArrayBuffer.isView(value)) {
|
|
262
|
+
return [value.buffer];
|
|
263
|
+
}
|
|
264
|
+
if (value && _typeof(value) === 'object') {
|
|
265
|
+
if (value[MARK]) {
|
|
266
|
+
return [value.bytes.buffer];
|
|
267
|
+
}
|
|
268
|
+
return Object.values(value).flatMap(function (part) {
|
|
269
|
+
return ArrayBuffer.isView(part) ? [part.buffer] : part && _typeof(part) === 'object' && part[MARK] ? [part.bytes.buffer] : [];
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
return [];
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// Where the options sit among each method's arguments, to give them the call's signal and progress callback.
|
|
276
|
+
var optionsIndex = {
|
|
277
|
+
getClustering: 1,
|
|
278
|
+
getPalette: 2,
|
|
279
|
+
getSwatches: 2,
|
|
280
|
+
quantize: 2,
|
|
281
|
+
quantizeFromClustering: 3,
|
|
282
|
+
quantizeWithPalette: 2,
|
|
283
|
+
quantizeToIndices: 2,
|
|
284
|
+
quantizeToIndicesFromClustering: 3,
|
|
285
|
+
quantizeToIndicesWithPalette: 2,
|
|
286
|
+
cluster: 2,
|
|
287
|
+
handleQuantize: 2,
|
|
288
|
+
handleQuantizeToIndices: 2,
|
|
289
|
+
handleQuantizeWithPalette: 2,
|
|
290
|
+
handleQuantizeToIndicesWithPalette: 2
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
// One AbortController per call that can be cancelled, by call id. It exists from the moment the call arrives, so a call
|
|
294
|
+
// still waiting its turn can be cancelled and never starts.
|
|
295
|
+
var controllers = new Map();
|
|
296
|
+
var respond = /*#__PURE__*/function () {
|
|
297
|
+
var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10(_ref4, post) {
|
|
298
|
+
var id, method, args, progress, controller, index, options, result, _t12, _t13, _t14;
|
|
299
|
+
return _regenerator().w(function (_context10) {
|
|
300
|
+
while (1) switch (_context10.n) {
|
|
301
|
+
case 0:
|
|
302
|
+
id = _ref4.id, method = _ref4.method, args = _ref4.args, progress = _ref4.progress;
|
|
303
|
+
controller = controllers.get(id);
|
|
304
|
+
_context10.p = 1;
|
|
305
|
+
if (!(controller && controller.signal.aborted)) {
|
|
306
|
+
_context10.n = 2;
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
return _context10.a(2);
|
|
310
|
+
case 2:
|
|
311
|
+
if (Object.hasOwn(methods, method)) {
|
|
312
|
+
_context10.n = 3;
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
throw Object.assign(new Error("Unknown method '".concat(method, "'.")), {
|
|
316
|
+
code: 'invalid-argument'
|
|
317
|
+
});
|
|
318
|
+
case 3:
|
|
319
|
+
if (controller || progress) {
|
|
320
|
+
args = args.slice();
|
|
321
|
+
index = optionsIndex[method];
|
|
322
|
+
options = _objectSpread({}, args[index] || {});
|
|
323
|
+
if (controller) {
|
|
324
|
+
options.signal = controller.signal;
|
|
325
|
+
}
|
|
326
|
+
if (progress) {
|
|
327
|
+
options.onProgress = function (fraction) {
|
|
328
|
+
return post({
|
|
329
|
+
id: id,
|
|
330
|
+
progress: fraction
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
args[index] = options;
|
|
335
|
+
}
|
|
336
|
+
_t12 = mark;
|
|
337
|
+
_context10.n = 4;
|
|
338
|
+
return methods[method].apply(methods, _toConsumableArray(args.map(revive)));
|
|
339
|
+
case 4:
|
|
340
|
+
_t13 = _context10.v;
|
|
341
|
+
result = _t12(_t13);
|
|
342
|
+
post({
|
|
343
|
+
id: id,
|
|
344
|
+
result: result
|
|
345
|
+
}, _toConsumableArray(new Set(buffers(result))));
|
|
346
|
+
_context10.n = 7;
|
|
347
|
+
break;
|
|
348
|
+
case 5:
|
|
349
|
+
_context10.p = 5;
|
|
350
|
+
_t14 = _context10.v;
|
|
351
|
+
if (!(controller && controller.signal.aborted)) {
|
|
352
|
+
_context10.n = 6;
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
return _context10.a(2);
|
|
356
|
+
case 6:
|
|
357
|
+
// Errors cross as plain data: the caller's side rebuilds them with the same name, message and code.
|
|
358
|
+
post({
|
|
359
|
+
id: id,
|
|
360
|
+
error: _t14 instanceof Error ? {
|
|
361
|
+
name: _t14.name,
|
|
362
|
+
message: _t14.message,
|
|
363
|
+
code: _t14.code,
|
|
364
|
+
argument: _t14.argument,
|
|
365
|
+
stack: _t14.stack
|
|
366
|
+
} : {
|
|
367
|
+
name: 'Error',
|
|
368
|
+
message: String(_t14),
|
|
369
|
+
code: 'worker-failed'
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
case 7:
|
|
373
|
+
_context10.p = 7;
|
|
374
|
+
controllers["delete"](id);
|
|
375
|
+
return _context10.f(7);
|
|
376
|
+
case 8:
|
|
377
|
+
return _context10.a(2);
|
|
378
|
+
}
|
|
379
|
+
}, _callee10, null, [[1, 5, 7, 8]]);
|
|
380
|
+
}));
|
|
381
|
+
return function respond(_x24, _x25) {
|
|
382
|
+
return _ref5.apply(this, arguments);
|
|
383
|
+
};
|
|
384
|
+
}();
|
|
385
|
+
|
|
386
|
+
// Calls run one at a time, in the order they arrived. A call that takes several turns of the event loop (a clustering run
|
|
387
|
+
// in slices) lets messages in between, which is how a cancel reaches it; without the queue, later calls would run
|
|
388
|
+
// in between its slices instead of after it.
|
|
389
|
+
var queue = Promise.resolve();
|
|
390
|
+
var onMessage = function onMessage(data, post) {
|
|
391
|
+
if (data.cancel !== undefined) {
|
|
392
|
+
var controller = controllers.get(data.cancel);
|
|
393
|
+
if (controller) {
|
|
394
|
+
controller.abort();
|
|
395
|
+
}
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
if (data.cancellable) {
|
|
399
|
+
controllers.set(data.id, new AbortController());
|
|
400
|
+
}
|
|
401
|
+
// respond() answers every failure itself; this catch only keeps one unforeseen rejection (an error that can't be posted)
|
|
402
|
+
// from leaving the queue rejected, which would silently stop the thread from running any later call.
|
|
403
|
+
queue = queue.then(function () {
|
|
404
|
+
return respond(data, post);
|
|
405
|
+
})["catch"](function () {});
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
// No top-level await: bundlers build workers as classic scripts by default (Vite's 'iife'), which can't have it.
|
|
409
|
+
// Decided the way worker.js decides how it started this thread: where Node's APIs exist (Node, Deno, Bun), the thread was
|
|
410
|
+
// started through worker_threads and talks through parentPort. Bun also defines the browser's `self.postMessage`
|
|
411
|
+
// inside such a thread, so checking `self` first would listen on a channel the parent never writes to.
|
|
412
|
+
var isNode = typeof process !== 'undefined' && !!(process.versions && process.versions.node);
|
|
413
|
+
if (!isNode && typeof self !== 'undefined' && typeof self.postMessage === 'function' && typeof self.addEventListener === 'function') {
|
|
414
|
+
self.addEventListener('message', function (event) {
|
|
415
|
+
return onMessage(event.data, function (message, transfer) {
|
|
416
|
+
return self.postMessage(message, transfer);
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
} else {
|
|
420
|
+
// Built at run time so no bundler tries to resolve it (see worker.js).
|
|
421
|
+
import(/* webpackIgnore: true */ /* @vite-ignore */['node', 'worker_threads'].join(':')).then(function (_ref6) {
|
|
422
|
+
var parentPort = _ref6.parentPort;
|
|
423
|
+
parentPort.on('message', function (data) {
|
|
424
|
+
return onMessage(data, function (message, transfer) {
|
|
425
|
+
return parentPort.postMessage(message, transfer);
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
}
|
package/dist/worker.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// `[Symbol.asyncDispose]` on handles needs TypeScript's `esnext.disposable` lib (TypeScript 5.2 and newer); this
|
|
2
|
+
// directive pulls it in. Older TypeScript versions report it unless `skipLibCheck` is on, as it is in most setups.
|
|
3
|
+
/// <reference lib="esnext.disposable" />
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
ImageLike, RawImage, ClusteringOptions, MappingOptions, QuantizeOptions, QuantizeIndexOptions, MappingIndexOptions,
|
|
7
|
+
IndexedImage, ClusteringInfo, InitOptions, ImageResult, PaletteLike, Swatches,
|
|
8
|
+
} from './index.js';
|
|
9
|
+
import type { Palette, Clustering } from './index.js';
|
|
10
|
+
|
|
11
|
+
/** The same class as the main entry's `Palette`: flat RGB bytes that know their colour space. */
|
|
12
|
+
export { Palette } from './index.js';
|
|
13
|
+
|
|
14
|
+
/** The same class as the main entry's `Clustering`: the typed bytes of a clustering. */
|
|
15
|
+
export { Clustering } from './index.js';
|
|
16
|
+
|
|
17
|
+
/** The version of this library, as in its `package.json`: the same value as the main entry's `version`. */
|
|
18
|
+
export const version: string;
|
|
19
|
+
|
|
20
|
+
/** The version of the data this library stores (clusterings): the same value as the main entry's `formatVersion`. */
|
|
21
|
+
export const formatVersion: 3;
|
|
22
|
+
|
|
23
|
+
export type {
|
|
24
|
+
ImageFormat, RawImage, ImageLike, PaletteLike, PlatformImageData, ImageResult, WasmModule, ColorSpace, AlphaMode, ClusteringOptions, MappingOptions, QuantizeOptions,
|
|
25
|
+
ErrorCode, ErrorArgument, CodedError, InitOptions, RunOptions, CancelOptions, IndexOptions, QuantizeIndexOptions, MappingIndexOptions, IndexedImage, ClusteringInfo, ClusteringInfoV2, ClusteringInfoV3, Swatches,
|
|
26
|
+
} from './index.js';
|
|
27
|
+
|
|
28
|
+
/** Options for `createWorker`. */
|
|
29
|
+
export interface WorkerOptions {
|
|
30
|
+
/**
|
|
31
|
+
* `false` (default): images are copied to the worker. `true`: their pixel buffers are transferred instead,
|
|
32
|
+
* which is faster for large images but leaves the caller's buffer detached (empty) afterwards. Only pixels that
|
|
33
|
+
* are a whole buffer of their own are transferred: a view into part of a larger buffer (a subarray, a small Node
|
|
34
|
+
* Buffer) is copied, so the caller's other data in that buffer is never detached.
|
|
35
|
+
*/
|
|
36
|
+
transfer?: boolean;
|
|
37
|
+
/** Where the worker gets its WebAssembly module; see `InitOptions`. It is sent as the worker's first call. */
|
|
38
|
+
wasm?: InitOptions['wasm'];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** An image and its clustering, kept in the worker so re-cutting and re-mapping them copies nothing across. */
|
|
42
|
+
export interface ClusteringHandle<Image extends ImageLike = ImageLike> {
|
|
43
|
+
getClustering(): Promise<Clustering>;
|
|
44
|
+
getPalette(k: number): Promise<Palette>;
|
|
45
|
+
getSwatches(k: number): Promise<Swatches>;
|
|
46
|
+
getClusteringInfo(): Promise<ClusteringInfo>;
|
|
47
|
+
/** Maps the kept image to the k-colour palette. Returns the same type as the image given to `cluster`. */
|
|
48
|
+
quantize(k: number, options?: MappingOptions): Promise<Image>;
|
|
49
|
+
quantizeToIndices(k: number, options?: MappingIndexOptions): Promise<IndexedImage>;
|
|
50
|
+
/** Maps the kept image to a palette of your own (an edited swatch, a brand palette) without sending the pixels again. */
|
|
51
|
+
quantizeWithPalette(palette: PaletteLike, options?: MappingOptions): Promise<Image>;
|
|
52
|
+
quantizeToIndicesWithPalette(palette: PaletteLike, options?: MappingIndexOptions): Promise<IndexedImage>;
|
|
53
|
+
/**
|
|
54
|
+
* Frees the image and clustering in the worker. Later calls on the handle fail. Call it when you are done, or
|
|
55
|
+
* declare the handle with `await using`, which does. A handle that is dropped without either is freed once it is
|
|
56
|
+
* garbage-collected, but when that happens is up to the JavaScript engine.
|
|
57
|
+
*/
|
|
58
|
+
release(): Promise<void>;
|
|
59
|
+
/** The same as `release()`, for `await using`. Present where the runtime has `Symbol.asyncDispose`. */
|
|
60
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The main entry's functions, run in a worker, plus handles and `terminate`. */
|
|
64
|
+
export interface AgglomerativeWorker {
|
|
65
|
+
/** Loads the module in the worker. `wasm` is the only option, and `createWorker({ wasm })` does the same. */
|
|
66
|
+
init(options?: InitOptions): Promise<void>;
|
|
67
|
+
getClustering(image: ImageLike, options?: ClusteringOptions): Promise<Clustering>;
|
|
68
|
+
getPalette(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Palette>;
|
|
69
|
+
getPaletteFromClustering(clustering: Clustering, k: number): Promise<Palette>;
|
|
70
|
+
getSwatches(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Swatches>;
|
|
71
|
+
getSwatchesFromClustering(clustering: Clustering, k: number): Promise<Swatches>;
|
|
72
|
+
getClusteringInfo(clustering: Clustering): Promise<ClusteringInfo>;
|
|
73
|
+
quantize<Image extends ImageLike>(image: Image, k: number, options?: QuantizeOptions): Promise<ImageResult<Image>>;
|
|
74
|
+
quantizeFromClustering<Image extends ImageLike>(image: Image, clustering: Clustering, k: number, options?: MappingOptions): Promise<ImageResult<Image>>;
|
|
75
|
+
quantizeWithPalette<Image extends ImageLike>(image: Image, palette: PaletteLike, options?: MappingOptions): Promise<ImageResult<Image>>;
|
|
76
|
+
quantizeToIndices(image: ImageLike, k: number, options?: QuantizeIndexOptions): Promise<IndexedImage>;
|
|
77
|
+
quantizeToIndicesFromClustering(image: ImageLike, clustering: Clustering, k: number, options?: MappingIndexOptions): Promise<IndexedImage>;
|
|
78
|
+
quantizeToIndicesWithPalette(image: ImageLike, palette: PaletteLike, options?: MappingIndexOptions): Promise<IndexedImage>;
|
|
79
|
+
/** Clusters `image` and keeps it, with its clustering, in the worker. */
|
|
80
|
+
cluster<Image extends ImageLike>(image: Image, options?: ClusteringOptions): Promise<ClusteringHandle<ImageResult<Image>>>;
|
|
81
|
+
/** Stops the worker. Calls still waiting are rejected, and later calls fail. */
|
|
82
|
+
terminate(): Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Starts a worker: a Web Worker in browsers, a worker thread in Node. It never keeps a Node process alive while idle. */
|
|
86
|
+
export function createWorker(options?: WorkerOptions): AgglomerativeWorker;
|
|
87
|
+
|
|
88
|
+
/** Stops the shared worker used by the exports below, rejecting calls still waiting. The next call starts a new one. */
|
|
89
|
+
export function terminate(): Promise<void>;
|
|
90
|
+
|
|
91
|
+
// The main entry's functions, run in one shared worker started on first use.
|
|
92
|
+
/** The main entry's `init`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
93
|
+
export const init: AgglomerativeWorker['init'];
|
|
94
|
+
/** The main entry's `getClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
95
|
+
export const getClustering: AgglomerativeWorker['getClustering'];
|
|
96
|
+
/** The main entry's `getPalette`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
97
|
+
export const getPalette: AgglomerativeWorker['getPalette'];
|
|
98
|
+
/** The main entry's `getPaletteFromClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
99
|
+
export const getPaletteFromClustering: AgglomerativeWorker['getPaletteFromClustering'];
|
|
100
|
+
/** The main entry's `getSwatches`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
101
|
+
export const getSwatches: AgglomerativeWorker['getSwatches'];
|
|
102
|
+
/** The main entry's `getSwatchesFromClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
103
|
+
export const getSwatchesFromClustering: AgglomerativeWorker['getSwatchesFromClustering'];
|
|
104
|
+
/** The main entry's `getClusteringInfo`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
105
|
+
export const getClusteringInfo: AgglomerativeWorker['getClusteringInfo'];
|
|
106
|
+
/** The main entry's `quantize`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
107
|
+
export const quantize: AgglomerativeWorker['quantize'];
|
|
108
|
+
/** The main entry's `quantizeFromClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
109
|
+
export const quantizeFromClustering: AgglomerativeWorker['quantizeFromClustering'];
|
|
110
|
+
/** The main entry's `quantizeWithPalette`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
111
|
+
export const quantizeWithPalette: AgglomerativeWorker['quantizeWithPalette'];
|
|
112
|
+
/** The main entry's `quantizeToIndices`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
113
|
+
export const quantizeToIndices: AgglomerativeWorker['quantizeToIndices'];
|
|
114
|
+
/** The main entry's `quantizeToIndicesFromClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
115
|
+
export const quantizeToIndicesFromClustering: AgglomerativeWorker['quantizeToIndicesFromClustering'];
|
|
116
|
+
/** The main entry's `quantizeToIndicesWithPalette`, run in the shared worker (see `createWorker` for a worker of your own). */
|
|
117
|
+
export const quantizeToIndicesWithPalette: AgglomerativeWorker['quantizeToIndicesWithPalette'];
|