UNPKG

agglomerative-clustering

Version:

Fast hierarchical agglomerative clustering powered by WebAssembly.

334 lines 15.7 kB
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); } 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 }; })(); } 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); } 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); } 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); }); }; } import createWasmModule from './clustering.js'; var codes = { 'rgba': 0, 'rgb': 1 }; var Module = null; export var init = /*#__PURE__*/function () { var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() { return _regenerator().w(function (_context) { while (1) switch (_context.n) { case 0: if (Module) { _context.n = 2; break; } _context.n = 1; return createWasmModule(); case 1: Module = _context.v; case 2: return _context.a(2); } }, _callee); })); return function init() { return _ref.apply(this, arguments); }; }(); var unpack = function unpack(pointer) { var lengthBuffer = new Uint8Array(Module.HEAPU8.subarray(pointer, pointer + 4)); var length = lengthBuffer[0] | lengthBuffer[1] << 8 | lengthBuffer[2] << 16 | lengthBuffer[3] << 24; var outputBuffer = new Uint8Array(Module.HEAPU8.subarray(pointer + 4, pointer + 4 + length)); return outputBuffer; }; var load = function load(image) { if (image instanceof Uint8Array) { return { data: image, format: 'rgba' }; } if (typeof ImageData !== 'undefined' && image instanceof ImageData) { return { data: new Uint8Array(image.data.buffer), format: 'rgba' }; } if (_typeof(image) !== 'object' || !image || !image.data || !image.format) { throw new Error("Invalid image: expected object with `data` and `format`."); } if (!(image.data instanceof Uint8Array) && !(image.data instanceof Uint8ClampedArray)) { throw new Error("Invalid image data: must be Uint8Array or Uint8ClampedArray."); } if (!Object.keys(codes).includes(image.format)) { throw new Error("Invalid format: must be 'rgb' or 'rgba'."); } return { data: image.data instanceof Uint8ClampedArray ? new Uint8Array(image.data.buffer) : image.data, format: image.format }; }; var getWrapper = function getWrapper(image) { if (typeof ImageData !== 'undefined' && image instanceof ImageData) { var width = image.width, height = image.height; return function (output) { return new ImageData(new Uint8ClampedArray(output.buffer), width, height); }; } if (!(image instanceof Uint8Array) && _typeof(image) === 'object' && image && image.data && image.format) { var format = image.format; return function (output) { return { data: output, format: format }; }; } return function (output) { return output; }; }; var check = function check(k) { if (typeof k !== 'number' || !Number.isInteger(k) || k <= 0) { throw new Error("Invalid k: must be a strictly positive integer."); } }; export var getClustering = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(image) { var imagePointer, outputPointer; return _regenerator().w(function (_context2) { while (1) switch (_context2.n) { case 0: _context2.n = 1; return init(); case 1: image = load(image); imagePointer = Module._malloc(image.data.length); if (imagePointer) { _context2.n = 2; break; } throw new Error("Failed to allocate WASM memory."); case 2: outputPointer = 0; _context2.p = 3; Module.HEAPU8.set(image.data, imagePointer); outputPointer = Module._get_clustering(imagePointer, image.data.length, codes[image.format]); return _context2.a(2, unpack(outputPointer)); case 4: _context2.p = 4; Module._free(imagePointer); if (outputPointer) Module._free(outputPointer); return _context2.f(4); case 5: return _context2.a(2); } }, _callee2, null, [[3,, 4, 5]]); })); return function getClustering(_x) { return _ref2.apply(this, arguments); }; }(); export var getPalette = /*#__PURE__*/function () { var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(image, k) { var imagePointer, outputPointer; return _regenerator().w(function (_context3) { while (1) switch (_context3.n) { case 0: _context3.n = 1; return init(); case 1: image = load(image); check(k); imagePointer = Module._malloc(image.data.length); if (imagePointer) { _context3.n = 2; break; } throw new Error("Failed to allocate WASM memory."); case 2: outputPointer = 0; _context3.p = 3; Module.HEAPU8.set(image.data, imagePointer); outputPointer = Module._get_palette(imagePointer, image.data.length, codes[image.format], k); return _context3.a(2, unpack(outputPointer)); case 4: _context3.p = 4; Module._free(imagePointer); if (outputPointer) Module._free(outputPointer); return _context3.f(4); case 5: return _context3.a(2); } }, _callee3, null, [[3,, 4, 5]]); })); return function getPalette(_x2, _x3) { return _ref3.apply(this, arguments); }; }(); export var getPaletteFromClustering = /*#__PURE__*/function () { var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(clustering, k) { var clusteringPointer, outputPointer; return _regenerator().w(function (_context4) { while (1) switch (_context4.n) { case 0: _context4.n = 1; return init(); case 1: check(k); clusteringPointer = Module._malloc(clustering.length); if (clusteringPointer) { _context4.n = 2; break; } throw new Error("Failed to allocate WASM memory."); case 2: outputPointer = 0; _context4.p = 3; Module.HEAPU8.set(clustering, clusteringPointer); outputPointer = Module._get_palette_from_clustering(clusteringPointer, clustering.length, k); return _context4.a(2, unpack(outputPointer)); case 4: _context4.p = 4; Module._free(clusteringPointer); if (outputPointer) Module._free(outputPointer); return _context4.f(4); case 5: return _context4.a(2); } }, _callee4, null, [[3,, 4, 5]]); })); return function getPaletteFromClustering(_x4, _x5) { return _ref4.apply(this, arguments); }; }(); export var quantize = /*#__PURE__*/function () { var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(image, k) { var wrap, imagePointer, outputPointer; return _regenerator().w(function (_context5) { while (1) switch (_context5.n) { case 0: _context5.n = 1; return init(); case 1: wrap = getWrapper(image); image = load(image); check(k); imagePointer = Module._malloc(image.data.length); if (imagePointer) { _context5.n = 2; break; } throw new Error("Failed to allocate WASM memory."); case 2: outputPointer = 0; _context5.p = 3; Module.HEAPU8.set(image.data, imagePointer); outputPointer = Module._quantize(imagePointer, image.data.length, codes[image.format], k); return _context5.a(2, wrap(unpack(outputPointer))); case 4: _context5.p = 4; Module._free(imagePointer); if (outputPointer) Module._free(outputPointer); return _context5.f(4); case 5: return _context5.a(2); } }, _callee5, null, [[3,, 4, 5]]); })); return function quantize(_x6, _x7) { return _ref5.apply(this, arguments); }; }(); export var quantizeWithClustering = /*#__PURE__*/function () { var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(image, clustering, k) { var wrap, imagePointer, clusteringPointer, outputPointer; return _regenerator().w(function (_context6) { while (1) switch (_context6.n) { case 0: _context6.n = 1; return init(); case 1: wrap = getWrapper(image); image = load(image); check(k); imagePointer = Module._malloc(image.data.length); if (imagePointer) { _context6.n = 2; break; } throw new Error("Failed to allocate WASM memory."); case 2: clusteringPointer = Module._malloc(clustering.length); if (clusteringPointer) { _context6.n = 3; break; } Module._free(imagePointer); throw new Error("Failed to allocate WASM memory."); case 3: outputPointer = 0; _context6.p = 4; Module.HEAPU8.set(image.data, imagePointer); Module.HEAPU8.set(clustering, clusteringPointer); outputPointer = Module._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k); return _context6.a(2, wrap(unpack(outputPointer))); case 5: _context6.p = 5; Module._free(imagePointer); Module._free(clusteringPointer); if (outputPointer) Module._free(outputPointer); return _context6.f(5); case 6: return _context6.a(2); } }, _callee6, null, [[4,, 5, 6]]); })); return function quantizeWithClustering(_x8, _x9, _x0) { return _ref6.apply(this, arguments); }; }(); export var quantizeWithPalette = /*#__PURE__*/function () { var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(image, palette) { var wrap, imagePointer, palettePointer, outputPointer; return _regenerator().w(function (_context7) { while (1) switch (_context7.n) { case 0: _context7.n = 1; return init(); case 1: wrap = getWrapper(image); image = load(image); imagePointer = Module._malloc(image.data.length); if (imagePointer) { _context7.n = 2; break; } throw new Error("Failed to allocate WASM memory."); case 2: palettePointer = Module._malloc(palette.length); if (palettePointer) { _context7.n = 3; break; } Module._free(imagePointer); throw new Error("Failed to allocate WASM memory."); case 3: outputPointer = 0; _context7.p = 4; Module.HEAPU8.set(image.data, imagePointer); Module.HEAPU8.set(palette, palettePointer); outputPointer = Module._quantize_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length); return _context7.a(2, wrap(unpack(outputPointer))); case 5: _context7.p = 5; Module._free(imagePointer); Module._free(palettePointer); if (outputPointer) Module._free(outputPointer); return _context7.f(5); case 6: return _context7.a(2); } }, _callee7, null, [[4,, 5, 6]]); })); return function quantizeWithPalette(_x1, _x10) { return _ref7.apply(this, arguments); }; }();