agglomerative-clustering 1.1.4 → 2.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 +164 -67
- package/dist/clustering.js +14 -2
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +182 -161
- package/dist/index.d.ts +61 -11
- package/dist/index.mjs +182 -161
- package/package.json +8 -3
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
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; } } }; }
|
|
2
|
+
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; } }
|
|
3
|
+
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; }
|
|
1
4
|
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); }
|
|
2
5
|
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 }; })(); }
|
|
3
6
|
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); }
|
|
@@ -8,6 +11,14 @@ var codes = {
|
|
|
8
11
|
'rgba': 0,
|
|
9
12
|
'rgb': 1
|
|
10
13
|
};
|
|
14
|
+
var colorSpaces = {
|
|
15
|
+
'rgb': 0,
|
|
16
|
+
'oklab': 1
|
|
17
|
+
};
|
|
18
|
+
var alphaModes = {
|
|
19
|
+
'ignore': 0,
|
|
20
|
+
'weight': 1
|
|
21
|
+
};
|
|
11
22
|
var Module = null;
|
|
12
23
|
export var init = /*#__PURE__*/function () {
|
|
13
24
|
var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
@@ -47,7 +58,8 @@ var load = function load(image) {
|
|
|
47
58
|
if (typeof ImageData !== 'undefined' && image instanceof ImageData) {
|
|
48
59
|
return {
|
|
49
60
|
data: new Uint8Array(image.data.buffer),
|
|
50
|
-
format: 'rgba'
|
|
61
|
+
format: 'rgba',
|
|
62
|
+
width: image.width
|
|
51
63
|
};
|
|
52
64
|
}
|
|
53
65
|
if (_typeof(image) !== 'object' || !image || !image.data || !image.format) {
|
|
@@ -61,7 +73,8 @@ var load = function load(image) {
|
|
|
61
73
|
}
|
|
62
74
|
return {
|
|
63
75
|
data: image.data instanceof Uint8ClampedArray ? new Uint8Array(image.data.buffer) : image.data,
|
|
64
|
-
format: image.format
|
|
76
|
+
format: image.format,
|
|
77
|
+
width: image.width
|
|
65
78
|
};
|
|
66
79
|
};
|
|
67
80
|
var getWrapper = function getWrapper(image) {
|
|
@@ -73,12 +86,21 @@ var getWrapper = function getWrapper(image) {
|
|
|
73
86
|
};
|
|
74
87
|
}
|
|
75
88
|
if (!(image instanceof Uint8Array) && _typeof(image) === 'object' && image && image.data && image.format) {
|
|
76
|
-
var format = image.format
|
|
89
|
+
var format = image.format,
|
|
90
|
+
_width = image.width,
|
|
91
|
+
_height = image.height;
|
|
77
92
|
return function (output) {
|
|
78
|
-
|
|
93
|
+
var result = {
|
|
79
94
|
data: output,
|
|
80
95
|
format: format
|
|
81
96
|
};
|
|
97
|
+
if (_width !== undefined) {
|
|
98
|
+
result.width = _width;
|
|
99
|
+
}
|
|
100
|
+
if (_height !== undefined) {
|
|
101
|
+
result.height = _height;
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
82
104
|
};
|
|
83
105
|
}
|
|
84
106
|
return function (output) {
|
|
@@ -90,9 +112,117 @@ var check = function check(k) {
|
|
|
90
112
|
throw new Error("Invalid k: must be a strictly positive integer.");
|
|
91
113
|
}
|
|
92
114
|
};
|
|
115
|
+
|
|
116
|
+
// Validates `options` against the keys a function accepts and fills in defaults:
|
|
117
|
+
// colorSpace: 'rgb' (default) or 'oklab', the space colours are clustered and matched in; when mapping with a
|
|
118
|
+
// clustering, the default is the space the clustering was made in (code -1)
|
|
119
|
+
// alpha: 'weight' (default) counts RGBA pixels in proportion to their opacity; 'ignore' counts every pixel
|
|
120
|
+
// precision: bits per channel, 1-8 (default 8, exact); lower values group similar colours before clustering
|
|
121
|
+
// dither: false (default), true, or a strength in [0, 1]; error diffusion when mapping pixels
|
|
122
|
+
// width: image width in pixels, needed to dither a Uint8Array or a RawImage without `width`
|
|
123
|
+
var parseOptions = function parseOptions(options, allowed, image) {
|
|
124
|
+
if (options === undefined) {
|
|
125
|
+
options = {};
|
|
126
|
+
}
|
|
127
|
+
if (options === null || _typeof(options) !== 'object' || Array.isArray(options)) {
|
|
128
|
+
throw new Error("Invalid options: expected an object.");
|
|
129
|
+
}
|
|
130
|
+
for (var _i = 0, _Object$keys = Object.keys(options); _i < _Object$keys.length; _i++) {
|
|
131
|
+
var key = _Object$keys[_i];
|
|
132
|
+
if (!allowed.includes(key)) {
|
|
133
|
+
throw new Error("Invalid option '".concat(key, "': expected one of ").concat(allowed.join(', '), "."));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
var colorSpace = options.colorSpace === undefined ? 'rgb' : options.colorSpace;
|
|
137
|
+
if (!Object.keys(colorSpaces).includes(colorSpace)) {
|
|
138
|
+
throw new Error("Invalid colorSpace: must be 'rgb' or 'oklab'.");
|
|
139
|
+
}
|
|
140
|
+
var alpha = options.alpha === undefined ? 'weight' : options.alpha;
|
|
141
|
+
if (!Object.keys(alphaModes).includes(alpha)) {
|
|
142
|
+
throw new Error("Invalid alpha: must be 'weight' or 'ignore'.");
|
|
143
|
+
}
|
|
144
|
+
var precision = options.precision === undefined ? 8 : options.precision;
|
|
145
|
+
if (typeof precision !== 'number' || !Number.isInteger(precision) || precision < 1 || precision > 8) {
|
|
146
|
+
throw new Error("Invalid precision: must be an integer from 1 to 8.");
|
|
147
|
+
}
|
|
148
|
+
var dither = options.dither === undefined || options.dither === false ? 0 : options.dither === true ? 1 : options.dither;
|
|
149
|
+
if (typeof dither !== 'number' || !Number.isFinite(dither) || dither < 0 || dither > 1) {
|
|
150
|
+
throw new Error("Invalid dither: must be a boolean or a number from 0 to 1.");
|
|
151
|
+
}
|
|
152
|
+
var width = options.width !== undefined ? options.width : image ? image.width : undefined;
|
|
153
|
+
if (dither > 0) {
|
|
154
|
+
var channels = image.format === 'rgb' ? 3 : 4;
|
|
155
|
+
if (typeof width !== 'number' || !Number.isInteger(width) || width <= 0) {
|
|
156
|
+
throw new Error("Invalid width: dithering needs the image width, as `options.width` or the image's `width`.");
|
|
157
|
+
}
|
|
158
|
+
if (image.data.length / channels % width !== 0) {
|
|
159
|
+
throw new Error("Invalid width: the image's pixel count is not a multiple of it.");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
colorSpace: colorSpaces[colorSpace],
|
|
164
|
+
colorSpaceGiven: options.colorSpace !== undefined,
|
|
165
|
+
alpha: alphaModes[alpha],
|
|
166
|
+
precision: precision,
|
|
167
|
+
dither: dither,
|
|
168
|
+
width: dither > 0 ? width : 0
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
var clusterOptions = ['colorSpace', 'precision', 'alpha'];
|
|
172
|
+
var quantizeOptions = ['colorSpace', 'precision', 'alpha', 'dither', 'width'];
|
|
173
|
+
var mapOptions = ['colorSpace', 'dither', 'width'];
|
|
174
|
+
|
|
175
|
+
// Clusterings are 0 or 3 bytes more than a multiple of 9 (1.x, no header), or 4 or 7 more with the 2.x header.
|
|
176
|
+
var checkClustering = function checkClustering(clustering) {
|
|
177
|
+
if (!(clustering instanceof Uint8Array)) {
|
|
178
|
+
throw new Error("Invalid clustering: must be a Uint8Array from getClustering.");
|
|
179
|
+
}
|
|
180
|
+
var rest = clustering.length % 9;
|
|
181
|
+
var legacy = clustering.length === 0 || rest === 3;
|
|
182
|
+
var current = (clustering.length === 4 || rest === 7) && clustering[0] === 0xAC && clustering[1] === 2 && clustering[2] <= 1;
|
|
183
|
+
if (!legacy && !current) {
|
|
184
|
+
throw new Error("Invalid clustering: not a clustering from getClustering.");
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Copies each buffer into the WASM heap, calls `fn` with their pointers, and returns the unpacked output,
|
|
189
|
+
// freeing everything afterwards.
|
|
190
|
+
var call = function call(buffers, fn) {
|
|
191
|
+
var pointers = [];
|
|
192
|
+
var outputPointer = 0;
|
|
193
|
+
try {
|
|
194
|
+
var _iterator = _createForOfIteratorHelper(buffers),
|
|
195
|
+
_step;
|
|
196
|
+
try {
|
|
197
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
198
|
+
var buffer = _step.value;
|
|
199
|
+
var pointer = Module._malloc(buffer.length);
|
|
200
|
+
if (!pointer) {
|
|
201
|
+
throw new Error("Failed to allocate WASM memory.");
|
|
202
|
+
}
|
|
203
|
+
pointers.push(pointer);
|
|
204
|
+
Module.HEAPU8.set(buffer, pointer);
|
|
205
|
+
}
|
|
206
|
+
} catch (err) {
|
|
207
|
+
_iterator.e(err);
|
|
208
|
+
} finally {
|
|
209
|
+
_iterator.f();
|
|
210
|
+
}
|
|
211
|
+
outputPointer = fn.apply(void 0, pointers);
|
|
212
|
+
return unpack(outputPointer);
|
|
213
|
+
} finally {
|
|
214
|
+
for (var _i2 = 0, _pointers = pointers; _i2 < _pointers.length; _i2++) {
|
|
215
|
+
var _pointer = _pointers[_i2];
|
|
216
|
+
Module._free(_pointer);
|
|
217
|
+
}
|
|
218
|
+
if (outputPointer) {
|
|
219
|
+
Module._free(outputPointer);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
};
|
|
93
223
|
export var getClustering = /*#__PURE__*/function () {
|
|
94
|
-
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(image) {
|
|
95
|
-
var
|
|
224
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(image, options) {
|
|
225
|
+
var o;
|
|
96
226
|
return _regenerator().w(function (_context2) {
|
|
97
227
|
while (1) switch (_context2.n) {
|
|
98
228
|
case 0:
|
|
@@ -100,35 +230,20 @@ export var getClustering = /*#__PURE__*/function () {
|
|
|
100
230
|
return init();
|
|
101
231
|
case 1:
|
|
102
232
|
image = load(image);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
throw new Error("Failed to allocate WASM memory.");
|
|
109
|
-
case 2:
|
|
110
|
-
outputPointer = 0;
|
|
111
|
-
_context2.p = 3;
|
|
112
|
-
Module.HEAPU8.set(image.data, imagePointer);
|
|
113
|
-
outputPointer = Module._get_clustering(imagePointer, image.data.length, codes[image.format]);
|
|
114
|
-
return _context2.a(2, unpack(outputPointer));
|
|
115
|
-
case 4:
|
|
116
|
-
_context2.p = 4;
|
|
117
|
-
Module._free(imagePointer);
|
|
118
|
-
if (outputPointer) Module._free(outputPointer);
|
|
119
|
-
return _context2.f(4);
|
|
120
|
-
case 5:
|
|
121
|
-
return _context2.a(2);
|
|
233
|
+
o = parseOptions(options, clusterOptions, image);
|
|
234
|
+
return _context2.a(2, call([image.data], function (imagePointer) {
|
|
235
|
+
return Module._get_clustering(imagePointer, image.data.length, codes[image.format], o.colorSpace, o.precision, o.alpha);
|
|
236
|
+
}));
|
|
122
237
|
}
|
|
123
|
-
}, _callee2
|
|
238
|
+
}, _callee2);
|
|
124
239
|
}));
|
|
125
|
-
return function getClustering(_x) {
|
|
240
|
+
return function getClustering(_x, _x2) {
|
|
126
241
|
return _ref2.apply(this, arguments);
|
|
127
242
|
};
|
|
128
243
|
}();
|
|
129
244
|
export var getPalette = /*#__PURE__*/function () {
|
|
130
|
-
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(image, k) {
|
|
131
|
-
var
|
|
245
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(image, k, options) {
|
|
246
|
+
var o;
|
|
132
247
|
return _regenerator().w(function (_context3) {
|
|
133
248
|
while (1) switch (_context3.n) {
|
|
134
249
|
case 0:
|
|
@@ -137,71 +252,40 @@ export var getPalette = /*#__PURE__*/function () {
|
|
|
137
252
|
case 1:
|
|
138
253
|
image = load(image);
|
|
139
254
|
check(k);
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
throw new Error("Failed to allocate WASM memory.");
|
|
146
|
-
case 2:
|
|
147
|
-
outputPointer = 0;
|
|
148
|
-
_context3.p = 3;
|
|
149
|
-
Module.HEAPU8.set(image.data, imagePointer);
|
|
150
|
-
outputPointer = Module._get_palette(imagePointer, image.data.length, codes[image.format], k);
|
|
151
|
-
return _context3.a(2, unpack(outputPointer));
|
|
152
|
-
case 4:
|
|
153
|
-
_context3.p = 4;
|
|
154
|
-
Module._free(imagePointer);
|
|
155
|
-
if (outputPointer) Module._free(outputPointer);
|
|
156
|
-
return _context3.f(4);
|
|
157
|
-
case 5:
|
|
158
|
-
return _context3.a(2);
|
|
255
|
+
o = parseOptions(options, clusterOptions, image);
|
|
256
|
+
return _context3.a(2, call([image.data], function (imagePointer) {
|
|
257
|
+
return Module._get_palette(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha);
|
|
258
|
+
}));
|
|
159
259
|
}
|
|
160
|
-
}, _callee3
|
|
260
|
+
}, _callee3);
|
|
161
261
|
}));
|
|
162
|
-
return function getPalette(
|
|
262
|
+
return function getPalette(_x3, _x4, _x5) {
|
|
163
263
|
return _ref3.apply(this, arguments);
|
|
164
264
|
};
|
|
165
265
|
}();
|
|
166
266
|
export var getPaletteFromClustering = /*#__PURE__*/function () {
|
|
167
267
|
var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(clustering, k) {
|
|
168
|
-
var clusteringPointer, outputPointer;
|
|
169
268
|
return _regenerator().w(function (_context4) {
|
|
170
269
|
while (1) switch (_context4.n) {
|
|
171
270
|
case 0:
|
|
172
271
|
_context4.n = 1;
|
|
173
272
|
return init();
|
|
174
273
|
case 1:
|
|
274
|
+
checkClustering(clustering);
|
|
175
275
|
check(k);
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
break;
|
|
180
|
-
}
|
|
181
|
-
throw new Error("Failed to allocate WASM memory.");
|
|
182
|
-
case 2:
|
|
183
|
-
outputPointer = 0;
|
|
184
|
-
_context4.p = 3;
|
|
185
|
-
Module.HEAPU8.set(clustering, clusteringPointer);
|
|
186
|
-
outputPointer = Module._get_palette_from_clustering(clusteringPointer, clustering.length, k);
|
|
187
|
-
return _context4.a(2, unpack(outputPointer));
|
|
188
|
-
case 4:
|
|
189
|
-
_context4.p = 4;
|
|
190
|
-
Module._free(clusteringPointer);
|
|
191
|
-
if (outputPointer) Module._free(outputPointer);
|
|
192
|
-
return _context4.f(4);
|
|
193
|
-
case 5:
|
|
194
|
-
return _context4.a(2);
|
|
276
|
+
return _context4.a(2, call([clustering], function (clusteringPointer) {
|
|
277
|
+
return Module._get_palette_from_clustering(clusteringPointer, clustering.length, k);
|
|
278
|
+
}));
|
|
195
279
|
}
|
|
196
|
-
}, _callee4
|
|
280
|
+
}, _callee4);
|
|
197
281
|
}));
|
|
198
|
-
return function getPaletteFromClustering(
|
|
282
|
+
return function getPaletteFromClustering(_x6, _x7) {
|
|
199
283
|
return _ref4.apply(this, arguments);
|
|
200
284
|
};
|
|
201
285
|
}();
|
|
202
286
|
export var quantize = /*#__PURE__*/function () {
|
|
203
|
-
var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(image, k) {
|
|
204
|
-
var wrap,
|
|
287
|
+
var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(image, k, options) {
|
|
288
|
+
var wrap, o;
|
|
205
289
|
return _regenerator().w(function (_context5) {
|
|
206
290
|
while (1) switch (_context5.n) {
|
|
207
291
|
case 0:
|
|
@@ -211,35 +295,20 @@ export var quantize = /*#__PURE__*/function () {
|
|
|
211
295
|
wrap = getWrapper(image);
|
|
212
296
|
image = load(image);
|
|
213
297
|
check(k);
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
throw new Error("Failed to allocate WASM memory.");
|
|
220
|
-
case 2:
|
|
221
|
-
outputPointer = 0;
|
|
222
|
-
_context5.p = 3;
|
|
223
|
-
Module.HEAPU8.set(image.data, imagePointer);
|
|
224
|
-
outputPointer = Module._quantize(imagePointer, image.data.length, codes[image.format], k);
|
|
225
|
-
return _context5.a(2, wrap(unpack(outputPointer)));
|
|
226
|
-
case 4:
|
|
227
|
-
_context5.p = 4;
|
|
228
|
-
Module._free(imagePointer);
|
|
229
|
-
if (outputPointer) Module._free(outputPointer);
|
|
230
|
-
return _context5.f(4);
|
|
231
|
-
case 5:
|
|
232
|
-
return _context5.a(2);
|
|
298
|
+
o = parseOptions(options, quantizeOptions, image);
|
|
299
|
+
return _context5.a(2, wrap(call([image.data], function (imagePointer) {
|
|
300
|
+
return Module._quantize(imagePointer, image.data.length, codes[image.format], k, o.colorSpace, o.precision, o.alpha, o.dither, o.width);
|
|
301
|
+
})));
|
|
233
302
|
}
|
|
234
|
-
}, _callee5
|
|
303
|
+
}, _callee5);
|
|
235
304
|
}));
|
|
236
|
-
return function quantize(
|
|
305
|
+
return function quantize(_x8, _x9, _x0) {
|
|
237
306
|
return _ref5.apply(this, arguments);
|
|
238
307
|
};
|
|
239
308
|
}();
|
|
240
309
|
export var quantizeWithClustering = /*#__PURE__*/function () {
|
|
241
|
-
var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(image, clustering, k) {
|
|
242
|
-
var wrap,
|
|
310
|
+
var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(image, clustering, k, options) {
|
|
311
|
+
var wrap, o, colorSpace;
|
|
243
312
|
return _regenerator().w(function (_context6) {
|
|
244
313
|
while (1) switch (_context6.n) {
|
|
245
314
|
case 0:
|
|
@@ -248,46 +317,23 @@ export var quantizeWithClustering = /*#__PURE__*/function () {
|
|
|
248
317
|
case 1:
|
|
249
318
|
wrap = getWrapper(image);
|
|
250
319
|
image = load(image);
|
|
320
|
+
checkClustering(clustering);
|
|
251
321
|
check(k);
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
throw new Error("Failed to allocate WASM memory.");
|
|
258
|
-
case 2:
|
|
259
|
-
clusteringPointer = Module._malloc(clustering.length);
|
|
260
|
-
if (clusteringPointer) {
|
|
261
|
-
_context6.n = 3;
|
|
262
|
-
break;
|
|
263
|
-
}
|
|
264
|
-
Module._free(imagePointer);
|
|
265
|
-
throw new Error("Failed to allocate WASM memory.");
|
|
266
|
-
case 3:
|
|
267
|
-
outputPointer = 0;
|
|
268
|
-
_context6.p = 4;
|
|
269
|
-
Module.HEAPU8.set(image.data, imagePointer);
|
|
270
|
-
Module.HEAPU8.set(clustering, clusteringPointer);
|
|
271
|
-
outputPointer = Module._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k);
|
|
272
|
-
return _context6.a(2, wrap(unpack(outputPointer)));
|
|
273
|
-
case 5:
|
|
274
|
-
_context6.p = 5;
|
|
275
|
-
Module._free(imagePointer);
|
|
276
|
-
Module._free(clusteringPointer);
|
|
277
|
-
if (outputPointer) Module._free(outputPointer);
|
|
278
|
-
return _context6.f(5);
|
|
279
|
-
case 6:
|
|
280
|
-
return _context6.a(2);
|
|
322
|
+
o = parseOptions(options, mapOptions, image);
|
|
323
|
+
colorSpace = o.colorSpaceGiven ? o.colorSpace : -1;
|
|
324
|
+
return _context6.a(2, wrap(call([image.data, clustering], function (imagePointer, clusteringPointer) {
|
|
325
|
+
return Module._quantize_with_clustering(imagePointer, image.data.length, codes[image.format], clusteringPointer, clustering.length, k, colorSpace, o.dither, o.width);
|
|
326
|
+
})));
|
|
281
327
|
}
|
|
282
|
-
}, _callee6
|
|
328
|
+
}, _callee6);
|
|
283
329
|
}));
|
|
284
|
-
return function quantizeWithClustering(
|
|
330
|
+
return function quantizeWithClustering(_x1, _x10, _x11, _x12) {
|
|
285
331
|
return _ref6.apply(this, arguments);
|
|
286
332
|
};
|
|
287
333
|
}();
|
|
288
334
|
export var quantizeWithPalette = /*#__PURE__*/function () {
|
|
289
|
-
var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(image, palette) {
|
|
290
|
-
var wrap,
|
|
335
|
+
var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(image, palette, options) {
|
|
336
|
+
var wrap, o;
|
|
291
337
|
return _regenerator().w(function (_context7) {
|
|
292
338
|
while (1) switch (_context7.n) {
|
|
293
339
|
case 0:
|
|
@@ -296,39 +342,14 @@ export var quantizeWithPalette = /*#__PURE__*/function () {
|
|
|
296
342
|
case 1:
|
|
297
343
|
wrap = getWrapper(image);
|
|
298
344
|
image = load(image);
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
throw new Error("Failed to allocate WASM memory.");
|
|
305
|
-
case 2:
|
|
306
|
-
palettePointer = Module._malloc(palette.length);
|
|
307
|
-
if (palettePointer) {
|
|
308
|
-
_context7.n = 3;
|
|
309
|
-
break;
|
|
310
|
-
}
|
|
311
|
-
Module._free(imagePointer);
|
|
312
|
-
throw new Error("Failed to allocate WASM memory.");
|
|
313
|
-
case 3:
|
|
314
|
-
outputPointer = 0;
|
|
315
|
-
_context7.p = 4;
|
|
316
|
-
Module.HEAPU8.set(image.data, imagePointer);
|
|
317
|
-
Module.HEAPU8.set(palette, palettePointer);
|
|
318
|
-
outputPointer = Module._quantize_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length);
|
|
319
|
-
return _context7.a(2, wrap(unpack(outputPointer)));
|
|
320
|
-
case 5:
|
|
321
|
-
_context7.p = 5;
|
|
322
|
-
Module._free(imagePointer);
|
|
323
|
-
Module._free(palettePointer);
|
|
324
|
-
if (outputPointer) Module._free(outputPointer);
|
|
325
|
-
return _context7.f(5);
|
|
326
|
-
case 6:
|
|
327
|
-
return _context7.a(2);
|
|
345
|
+
o = parseOptions(options, mapOptions, image);
|
|
346
|
+
return _context7.a(2, wrap(call([image.data, palette], function (imagePointer, palettePointer) {
|
|
347
|
+
return Module._quantize_with_palette(imagePointer, image.data.length, codes[image.format], palettePointer, palette.length, o.colorSpace, o.dither, o.width);
|
|
348
|
+
})));
|
|
328
349
|
}
|
|
329
|
-
}, _callee7
|
|
350
|
+
}, _callee7);
|
|
330
351
|
}));
|
|
331
|
-
return function quantizeWithPalette(
|
|
352
|
+
return function quantizeWithPalette(_x13, _x14, _x15) {
|
|
332
353
|
return _ref7.apply(this, arguments);
|
|
333
354
|
};
|
|
334
355
|
}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agglomerative-clustering",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-beta.1",
|
|
4
4
|
"description": "Fast hierarchical agglomerative clustering powered by WebAssembly.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Rohan Fredriksson",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"README.md"
|
|
44
44
|
],
|
|
45
45
|
"scripts": {
|
|
46
|
-
"build:wasm": "mkdirp dist &&
|
|
46
|
+
"build:wasm": "mkdirp dist && em++ src/clustering.cpp -O3 -msimd128 -s WASM=1 -s MODULARIZE=1 -s EXPORT_ES6=1 -s EXPORT_NAME='createWasmModule' -s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=32MB -s MAXIMUM_MEMORY=2147483648 -s EXPORTED_FUNCTIONS=\"['_get_clustering','_get_palette','_get_palette_from_clustering','_quantize','_quantize_with_clustering','_quantize_with_palette','_malloc','_free']\" -s EXPORTED_RUNTIME_METHODS=\"['HEAPU8']\" -o dist/clustering.js",
|
|
47
47
|
"build:cjs": "cross-env BABEL_ENV=cjs babel src --out-dir dist --extensions \".js\" --out-file-extension .cjs",
|
|
48
48
|
"build:esm": "cross-env BABEL_ENV=esm babel src --out-dir dist --extensions \".js\" --out-file-extension .mjs",
|
|
49
49
|
"build:types": "mkdirp dist && node -e \"require('fs').copyFileSync('src/index.d.ts', 'dist/index.d.ts')\"",
|
|
@@ -52,7 +52,10 @@
|
|
|
52
52
|
"prepublishOnly": "npm run build",
|
|
53
53
|
"test": "vitest run",
|
|
54
54
|
"bench": "vitest bench",
|
|
55
|
-
"
|
|
55
|
+
"bench:full": "node bench/run.js",
|
|
56
|
+
"bench:compare": "node bench/compare.js",
|
|
57
|
+
"quality": "node bench/run.js --no-pipeline --label quality",
|
|
58
|
+
"demo": "node demo/serve.mjs"
|
|
56
59
|
},
|
|
57
60
|
"devDependencies": {
|
|
58
61
|
"@babel/cli": "^7.27.2",
|
|
@@ -62,7 +65,9 @@
|
|
|
62
65
|
"canvas": "^3.2.3",
|
|
63
66
|
"cross-env": "^7.0.3",
|
|
64
67
|
"image-q": "^4.0.0",
|
|
68
|
+
"libimagequant-wasm": "^0.3.0",
|
|
65
69
|
"mkdirp": "^3.0.1",
|
|
70
|
+
"quantize": "^1.0.2",
|
|
66
71
|
"vitest": "^4.1.5"
|
|
67
72
|
}
|
|
68
73
|
}
|